为什么 UseStaticFiles 和 UseDefaultFiles 之间的顺序很重要? [英] Why does order between UseStaticFiles and UseDefaultFiles matter?

查看:33
本文介绍了为什么 UseStaticFiles 和 UseDefaultFiles 之间的顺序很重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解中间件的注册顺序可能很重要.然而,事实并非如此.

I understand that the order of registration for middleware may matter. However, it's not given that it's necessarily the case.

我注意到 UseDefaultFiles() 需要在 UseStaticFiles() 之前(这可以通过 UseFileServer()).

I noticed that UseDefaultFiles() needs to precede UseStaticFiles() (which can neatly be circumvented by UseFileServer()).

我不明白是为什么.它们是如何碰撞的?!

What I don't understand is why. How do they collide?!

我在谷歌上搜索了这个问题,但对于为什么在这种特殊情况下订单很重要的动机为零.只是它很重要...

I've googled the issue but got zero motivation on why the order is significant in this particular case. Only that it is of importance...

推荐答案

取自 ASP.NET Core 中的静态文件(在提供默认文档下,有一个重要说明).

Taken from the documentation on Static files in ASP.NET Core (under Serve a default document, there's an important note).

UseDefaultFiles 必须在 UseStaticFiles 之前调用以提供默认文件.UseDefaultFiles 是一个 URL 重写器,它实际上并不为文件提供服务.通过 UseStaticFiles 启用静态文件中间件来提供文件.

UseDefaultFiles must be called before UseStaticFiles to serve the default file. UseDefaultFiles is a URL rewriter that doesn't actually serve the file. Enable Static File Middleware via UseStaticFiles to serve the file.

基于此,在提供实际文件 (UseStaticFiles) 之前,首先设置 URL 重写器 (UseDefaultFiles) 很重要.
如果不这样做, UseStaticFiles 中间件将首先启动,但是对应用程序根目录的请求不会告诉中间件要提供哪个文件".当您首先确保重写到位时,对应用程序根目录的请求将被重写为对(其中一个)默认文件的请求.

Based on this, it's important to first setup the URL rewriter (UseDefaultFiles) before serving the actual file (UseStaticFiles).
If you don't, the UseStaticFiles middleware will kick in first, but a request to the root of the application won't tell the middleware which 'file' to serve. When you make sure the rewrite is in place first, a request to the root of the application will have been rewritten to be a request for (one of the) default file(s).

这篇关于为什么 UseStaticFiles 和 UseDefaultFiles 之间的顺序很重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆