ASP.NET Core映射到静态文件处理程序的路由 [英] ASP.NET Core map route to static file handler
问题描述
我正在使用Angular在ASP.NET Core网站(以前称为ASP.NET 5/vNext)上工作.为了使Angular工作,我需要有一条通吃的路线:
I'm working on a ASP.NET Core website (previously named ASP.NET 5 / vNext) with Angular. In order for Angular to work I need to have a catch-all route:
app.UseStaticFiles();
app.UseMvc(routes =>
{
// Angular fallback route
routes.MapRoute("angular", "{*url}", new { controller = "Home", action = "Index" });
});
我在wwwroot中也有一些文件/文件夹,例如:
I also have a few files/folders in wwwroot, like:
wwwroot/app
wwwroot/assets
wwwroot/lib
对这些路径进行任何请求时,例如http://example.com/assets/css/test.css
,并且文件(test.css
)不存在,则不应继续使用后备路由.它应该返回404.
When any requests are made to these paths, for example http://example.com/assets/css/test.css
, and the file (test.css
) does NOT exist, it should not continue to the fallback route. It should return a 404.
现在,如果文件不存在,它将返回Angular HTML.那么,我怎么能说以'/assets'开头的任何路径只能由UseStaticFiles
路由/提供服务?
Right now, if the file does not exist it returns the Angular HTML. So, how can I tell it that any path that starts with '/assets' should only be routed / served by UseStaticFiles
?
推荐答案
这似乎可行:
app.MapWhen(
context => {
var path = context.Request.Path.Value.ToLower();
return
path.StartsWith("/assets") ||
path.StartsWith("/lib") ||
path.StartsWith("/app");
},
config => config.UseStaticFiles());
但是,我不确定是否有任何性能(或其他类型的)影响.我会更新的.
However, I'm not sure if there are any performance (or other type of) implications. I'll update if I come across any.
这篇关于ASP.NET Core映射到静态文件处理程序的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!