简体   繁体   English

静态文件中间件应该放在ASP.NET核心管道中的哪个位置?

[英]Where should static files middleware be in the ASP.NET Core pipeline?

I'm using ASP.NET Core 2.1. 我正在使用ASP.NET Core 2.1。 I thought the static files middleware should come before the mvc middleware - no need to run a request through mvc just to serve a css file for example. 我认为静态文件中间件应该出现在mvc中间件之前 - 例如,不需要通过mvc运行请求来提供css文件。

So I have them in this order: 所以我按顺序排列:

app.UseExceptionHandler(/*...*/)
app.UseHsts();
app.UseHttpsRedirection();
app.UseStatusCodePagesWithReExecute(/*...*/);
// and lastly:
app.UseStaticFiles();
app.UseMvc(/*...*/);

However when I turn on debug level logging, I notice that if a static file is missing, it runs through Microsoft.AspNetCore.Builder.RouterMiddleware and says Request did not match any routes , then runs my ErrorController and issues a 404 for that request. 但是,当我打开调试级别日志记录时,我注意到如果缺少静态文件,它将通过Microsoft.AspNetCore.Builder.RouterMiddleware运行,并说Request did not match any routes ,然后运行我的ErrorController并为该请求发出404。

So: 所以:

  • is this the correct order for the pipeline? 这是管道的正确顺序吗?
  • is there a way to avoid all this, or is it by design? 有没有办法避免这一切,还是设计? eg Some "lighter" process to trigger a 404 without going through all that? 例如,一些“更轻”的过程触发404而不经历所有这些? Like maybe having static files middleware be first (not sure if that's wise/secure though)? 就像可能首先使用静态文件中间件(不确定这是否明智/安全)?

is this the correct order for the pipeline? 这是管道的正确顺序吗?

Yes, it is. 是的。

However when I turn on debug level logging, I notice that if a static file is missing, it runs through Microsoft.AspNetCore.Builder.RouterMiddleware and says Request did not match any routes , then runs my ErrorController and issues a 404 for that request. 但是,当我打开调试级别日志记录时,我注意到如果缺少静态文件,它将通过Microsoft.AspNetCore.Builder.RouterMiddleware运行,并说Request did not match any routes ,然后运行我的ErrorController并为该请求发出404。 Why? 为什么?

First, your missing static file request is going through exception handler, HSTS, HTTPS redirection and StatusCodePagesWithReExecute middleware, but let's ignore them, because there is nothing interesting. 首先,您丢失的静态文件请求将通过异常处理程序,HSTS,HTTPS重定向和StatusCodePagesWithReExecute中间件,但让我们忽略它们,因为没有什么有趣的。 Request just passes through them. 请求只是通过它们。

Then, it is processed by static files middleware. 然后,它由静态文件中间件处理。 The middleware soon understands, that file is missing and just lets your request run to next middleware , which is MVC middlware. 中间件很快就会理解,该文件丢失了,只是让你的请求运行到下一个中​​间件 ,即MVC middlware。

MVC middleware looks through its route table and finds "catchAll" route and lets ErrorController process the request. MVC中间件查看其路由表并找到“catchAll”路由并让ErrorController处理请求。 That is the reason why missing files are processed by ErrorController . 这就是ErrorController处理丢失文件的原因。

PS I suppose you have "catchAll" route something like this: PS我想你有“catchAll”路线这样的东西:

app.UseMvc(routes =>
        {
            .... // your routes here

            routes.MapRoute("catchAll", "{*.}", new { controller = "Error", action = "Error404" }
        });

To get it lighter you can have a custom middleware something like this: 为了让它更轻,你可以拥有这样的自定义中间件:

var avoidFolders = new string[] { "js", "css" };

app.Use(async (context, next) => {
    if (avoidFolders.Contains(context.Request.Path.Value.Trim('/')))
        context.Response.StatusCode = 404;
    else await next();
});

Although you will have to include every static folder in the array, it makes sure to directly return a 404 without proceeding to routing. 虽然您必须在数组中包含每个静态文件夹,但它确保直接返回404而不进行路由。

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

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