简体   繁体   中英

Exclude route from middleware - .net core

I have 2 middlewares in the application. I want to exclude one route from those middlewares. What i have tried is creating a BuildRouter functions and apply middlewares through it but this didn't work.

public IRouter BuildRouter(IApplicationBuilder applicationBuilder)
{
    var builder = new RouteBuilder(applicationBuilder);

    builder.MapMiddlewareRoute("/api/", appBuilder => {
        appBuilder.ApplyKeyValidation();
        appBuilder.ApplyPolicyValidation();
    });

    return builder.Build();
}

And the configure method is

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseRouter(BuildRouter(app));

    app.UseHttpsRedirection();
    app.UseMvc();       
}

But this is not working.

You can use the MapWhen extension method with a negative predicate:

app.MapWhen(
    httpContext => !httpContext.Request.Path.StartsWithSegments("/pathtoexclude"),
    subApp => subApp.UseMyMiddleware()
);

NOTE : MapWhen will terminate the pipeline, so if you want the pipeline to continue after this you can use app.UseWhen instead.

For middlewares, you should use app.UseWhen as opposed to app.MapWhen because MapWhen terminates the pipeline. I learned this the hard way trying to use the other answer.

It's been a while, but since I stumbled across this others might too. So, for your example: public void Configure(IApplicationBuilder app, IHostingEnvironment env)

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseHsts();
    }

    app.UseWhen(
        context => !context.Request.Path.StartsWithSegments("/api"),
        appBuilder =>
        {
            appBuilder.ApplyKeyValidation();
            appBuilder.ApplyPolicyValidation();
        }
    );

    app.UseHttpsRedirection();
    app.UseMvc();       
}

A simple adaptation to the brilliant answers here could be to make app config decisions based on the request. In my case, the fallback page for Blazor seemed to be handling all the requests made to middlewares like Swagger and Hangfire.

Conditionally calling endpoints.MapFallbackToPage("/_Host"); will bring back default support to all middlewares accessible via routes starting with /api . Below is a simplified example:

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) => ConfigureServer(app, env);

void ConfigureServer(IApplicationBuilder app, IWebHostEnvironment env, bool isApiBound = true)
{        
    //...

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        if (!isApiBound)
        {
            endpoints.MapRazorPages();
            endpoints.MapFallbackToPage("/_Host");
        }
    });

    //...

    if (isApiBound)
    {
        app.UseWhen(context => !context.Request.Path.StartsWithSegments("/api"),
            builder => ConfigureServer(builder, env, false));
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM