ASP.net core MVC catch all route serve static file

前端 未结 7 2378
悲&欢浪女
悲&欢浪女 2020-12-13 09:20

Is there a way to make a catch all route serve a static file?

Looking at this http://blog.nbellocam.me/2016/03/21/routing-angular-2-asp-net-core/

I basically

7条回答
  •  难免孤独
    2020-12-13 10:14

    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(...);
    

    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");
    }
    

提交回复
热议问题