MVC 6 Routing, SPA fallback + 404 Error Page

前端 未结 3 895

With RC1 of ASP.NET Core 1.0\'s MVC 6 you can map routes from within your Startup.Configure function when invoking app.UseMvc. I h

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-02 11:39

    Try this:

    public void Configure(IApplicationBuilder app,
        IHostingEnvironment env,
        ILoggerFactory loggerFactory)
    {
        app.UseExceptionHandler("/Home/Error");
        app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");
    
        app.UseMvc(routes =>
        {
            routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            routes.MapWebApiRoute("defaultApi", "api/{controller}/{id?}");
        });
    
        app.Run(async context =>
        {
            context.Response.Redirect("/Home/Index");
            await Task.CompletedTask;
        });
    }
    

    app.Run() will catch all requests that doesn't match any of the routes defined earlier. This way you can get your custom spa fallback and use app.UseStatusCodePagesWithRedirects() at the same time.

提交回复
热议问题