简体   繁体   English

从中间件中排除路由 - .net core

[英]Exclude route from middleware - .net core

I have 2 middlewares in the application.我在应用程序中有 2 个中间件。 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.我尝试过的是创建一个 BuildRouter 函数并通过它应用中间件,但这不起作用。

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:您可以使用带有否定谓词的MapWhen扩展方法:

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.注意MapWhen将终止管道,因此如果您希望管道在此之后继续,您可以改用app.UseWhen

For middlewares, you should use app.UseWhen as opposed to app.MapWhen because MapWhen terminates the pipeline.对于中间件,你应该使用app.UseWhen而不是app.MapWhen因为MapWhen终止管道。 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)

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.就我而言,Blazor 的后备页面似乎正在处理对 Swagger 和 Hangfire 等中间件的所有请求。

Conditionally calling endpoints.MapFallbackToPage("/_Host");有条件地调用endpoints.MapFallbackToPage("/_Host"); will bring back default support to all middlewares accessible via routes starting with /api .将为通过以/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));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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