【问题标题】:difference between Map and MapWhen branch in asp.net core Middleware?asp.net核心中间件中Map和MapWhen分支的区别?
【发布时间】:2018-06-08 09:22:00
【问题描述】:

当我们对请求进行身份验证时,何时在 asp.net 核心中间件中使用 Map 和 MapWhen 分支。

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.Map("", (appBuilder) =>
    {
        appBuilder.Run(async (context) => {
            await context.Response.WriteAsync("");
        });
    });

    app.MapWhen(context => context.Request.Query.ContainsKey(""), (appBuilder) =>
    {
        appBuilder.Run(async (context) =>
        {
            await context.Response.WriteAsync("");
        });

    });
}

【问题讨论】:

    标签: asp.net-core asp.net-core-middleware


    【解决方案1】:

    接受的答案很有帮助,但并不完全准确。除了谓词逻辑之外,MapMapWhen 之间的主要区别在于 Map 会将 MapMiddleware 添加到管道(参见 here),而 MapWhen 将添加 MapWhenMiddleware 到管道(参见here)。这样做的效果是Map 将更新Request.PathRequest.PathBase 以考虑基于路径的分支(从Request.Path 中修剪匹配的路径段并将其附加到Request.PathBase),而看似等效的@ 987654334@ 谓词不会。这会影响使用该路径的任何下游,例如路由!

    【讨论】:

    • 这应该是公认的答案。如果地图中的中间件需要路径的第一部分,则尤其如此。例如,如果UseMvc() 在路由中需要前缀/api,则app.Map("/api", builder => builder.UseMvc()) 将不起作用。相反,最好使用app.UseWhen(context => context.Request.Path.StartsWithSegments("/api"), ... );
    • 这个答案解释了使用.Map(的一个非常重要的方面
    【解决方案2】:

    Map 只能根据指定请求路径的匹配来分支请求。 MapWhen 更强大,它允许基于与当前HttpContext 对象一起操作的指定谓词的结果来分支请求。 到目前为止,HttpContext 包含有关 HTTP 请求的所有信息,MapWhen 允许您使用非常具体的条件来分支请求管道。

    任何Map 调用都可以轻松转换为MapWhen,但反之则不然。例如这个Map 电话:

    app.Map("SomePathMatch", (appBuilder) =>
    {
        appBuilder.Run(async (context) => {
    
            await context.Response.WriteAsync("");
        });
    });
    

    相当于下面的MapWhen调用:

    app.MapWhen(context => context.Request.Path.StartsWithSegments("SomePathMatch"), (appBuilder) =>
    {
        appBuilder.Run(async (context) =>
        {
            await context.Response.WriteAsync("");
        });
    });
    

    所以回答您的问题“何时使用 Map 和 MapWhen 分支”:当您仅基于请求路径分支请求时使用 Map。当您根据 HTTP 请求中的其他数据对请求进行分支时,请使用 MapWhen

    【讨论】:

    • CodeFuller 这并不完全正确,请参阅下面@alex-lorimer 的答案以及我的评论中的示例。
    猜你喜欢
    • 1970-01-01
    • 2014-04-06
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2019-02-17
    • 1970-01-01
    相关资源
    最近更新 更多