ASP.net core MVC catch all route serve static file(ASP.net core MVC 捕获所有路由服务静态文件)
问题描述
有没有办法让 catch all 路由服务于静态文件?
Is there a way to make a catch all route serve a static file?
看这个http://blog.nbellocam.me/2016/03/21/routing-angular-2-asp-net-core/
我基本上想要这样的东西:
I basically want something like this:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller}/{action=Index}");
routes.MapRoute("spa", "{*url}"); // This should serve SPA index.html
});
所以任何与 MVC 控制器不匹配的路由都会提供 wwwroot/index.html
So any route that doesn't match an MVC controller will serve up wwwroot/index.html
推荐答案
如果您已经处于路由阶段,那么您已经过了在管道中提供静态文件的阶段.您的初创公司将如下所示:
If you're already in the routing stage, you've gone past the point where static files are served in the pipeline. Your startup will look something like this:
app.UseStaticFiles();
...
app.UseMvc(...);
这里的顺序很重要.因此,您的应用将首先查找静态文件,这从性能的角度来看是有意义的 - 如果您只想丢弃静态文件,则无需运行 MVC 管道.
The order here is important. So your app will look for static files first, which makes sense from a performance standpoint - no need to run through MVC pipeline if you just want to throw out a static file.
您可以创建一个包罗万象的控制器操作,该操作将返回文件的内容.例如(窃取您评论中的代码):
You can create a catch-all controller action that will return the content of the file instead. For example (stealing the code in your comment):
public IActionResult Spa()
{
return File("~/index.html", "text/html");
}
这篇关于ASP.net core MVC 捕获所有路由服务静态文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ASP.net core MVC 捕获所有路由服务静态文件
基础教程推荐
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- WPF 模态进度窗口 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
