Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Map and MapWhen branch in asp.net core Middleware?

When to use Map and MapWhen branch in asp.net core middleware while we are authenticating request.

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("");
        });

    });
}
like image 341
Saineshwar Avatar asked Dec 27 '17 18:12

Saineshwar


People also ask

What is the use of the Map extension while adding middleware to the ASP.NET Core pipeline?

Branch the middleware pipeline Map extensions are used as a convention for branching the pipeline. Map branches the request pipeline based on matches of the given request path. If the request path starts with the given path, the branch is executed.

What are Middlewares in .NET Core?

Middleware are software components that are assembled into an application pipeline to handle requests and responses. Each component chooses whether to pass the request on to the next component in the pipeline, and can perform certain actions before and after the next component is invoked in the pipeline.

What is the difference between app use VS app run while adding middleware?

For app. Run , it adds a terminal middleware delegate to the application's request pipeline. For app. Use , it adds a middleware delegate to the application's request pipeline.

What is the use of UseRouting and UseEndpoints in startup configure method?

UseRouting adds route matching to the middleware pipeline. This middleware looks at the set of endpoints defined in the app, and selects the best match based on the request. UseEndpoints adds endpoint execution to the middleware pipeline. It runs the delegate associated with the selected endpoint.


2 Answers

The accepted answer is helpful, but not entirely accurate. Apart from the predicate logic, key differences between Map and MapWhen are that Map will add MapMiddleware to the pipeline (see here), while MapWhen will add MapWhenMiddleware to the pipeline (see here). The effect of this is that Map will update the Request.Path and Request.PathBase to account for branching based on path (trimming the matched path segment off Request.Path and appending it to Request.PathBase), while a seemingly equivalent MapWhen predicate will not. This affects anything downstream that uses the path, such as routing!

like image 184
Alex Lorimer Avatar answered Sep 28 '22 17:09

Alex Lorimer


Map could branch the request based on match of the specified request path only. MapWhen is more powerful and allows branching the request based on result of specified predicate that operates with current HttpContext object. As far HttpContext contains all information about HTTP request, MapWhen allows you to use very specific conditions for branching request pipeline.

Any Map call could be easily converted to MapWhen, but not vice versa. For example this Map call:

app.Map("SomePathMatch", (appBuilder) =>
{
    appBuilder.Run(async (context) => {

        await context.Response.WriteAsync("");
    });
});

is equivalent to the following MapWhen call:

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

So answering your question "When to use Map and MapWhen branch": use Map when you branch request based on request path only. Use MapWhen when you branch request based on other data from the HTTP request.

like image 31
CodeFuller Avatar answered Sep 28 '22 18:09

CodeFuller