故如虹,知恩;故如月,知明
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
欢迎加群交流技术

.NET CORE 3.1 URL 生成的使用,配合中间件

4175人阅读 2019/12/10 11:36 总访问:3842714 评论:0 收藏:0 手机
分类: .NET Core

URL 生成:

是指路由基于一系列路由值创建 URL 路径的过程。

允许终结点与访问它们的 URL 之间存在逻辑分隔。


常用方法

GetPathByAction

GetUriByAction

GetPathByPage

GetUriByPage


配合中间件使用,在中间件中测试获取,一般可以配合自定义路由来使用

namespace WebApplication1.MyMiddleware
{
    public class ProductsLinkMiddleware
    {
        private readonly LinkGenerator _linkGenerator;

        public ProductsLinkMiddleware(RequestDelegate next, LinkGenerator linkGenerator)
        {
            _linkGenerator = linkGenerator;
        }

        public async Task InvokeAsync(HttpContext httpContext)
        {
            //根据action得到路径
            var url = _linkGenerator.GetPathByAction("ListProducts", "Store");
            //根据actin得到完整的路径
            var url2 = _linkGenerator.GetUriByAction(httpContext, "ListProducts", "Store");

            httpContext.Response.ContentType = "text/plain";
            await httpContext.Response.WriteAsync($"Go to {url} to see our products.");
        }
    }
}

注入中间件:



欢迎加群讨论技术,群:677373950(满了,可以加,但通过不了),2群:656732739

评价