MVC 6路由,SPA后备+ 404错误页面

编程入门 行业动态 更新时间:2024-10-27 08:28:01
本文介绍了MVC 6路由,SPA后备+ 404错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 ASP的 RC1 .NET Core 1.0的MVC 6 ,您可以在调用app.UseMvc时从Startup.Configure函数中映射路由.我已经映射了一条"spa-fallback"路由,该路由将确保HomeController和Index视图为默认值,如下所示:

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 have mapped a "spa-fallback" route that will ensure that the HomeController and Index view are the defaults like so:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // ... omitted for brevity app.UseExceptionHandler("/Home/Error"); app.UseStatusCodePagesWithRedirects("/Home/Error/{0}"); app.UseMvc(routes => { routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}"); routes.MapRoute("spa-fallback", "{*anything}", new { controller = "Home", action = "Index" }); routes.MapWebApiRoute("defaultApi", "api/{controller}/{id?}"); }); }

我希望回退,以便我的 Angular2 应用的路由不会导致HTTP状态代码为 404(未找到).但是,当用户无意间尝试导航到不存在的页面视图时,我还需要正确处理.您可能会注意到我也叫app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");.

I desire the fallback so that my Angular2 app's routes will not result in an HTTP Status Code of 404, Not Found. But I also need to correctly handle when a user does inadvertently attempt to navigate to a page view that doesn't exist. You might notice that I have also called app.UseStatusCodePagesWithRedirects("/Home/Error/{0}");.

使用状态代码和"spa-fallback"路由重定向到我的错误页面的调用似乎是互斥的-意味着似乎我只能拥有一个或另一个(但不能同时拥有两个).有谁知道我要如何做到两全其美?

The call to redirect to my error page with the status code and the "spa-fallback" route seem mutually exclusive -- meaning it appears that I can only have one or the other (but sadly not both). Does anyone know how I could manage to have the best of both worlds?

推荐答案

我花了一些时间弄清楚如何在不使用MVC服务索引的情况下执行此操作,并且仍然收到缺少文件的404.这是我的http管道:

It took me some time to figure out how to do this without serving my index using MVC and to still receive 404s for missing files. Here's my http pipeline:

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc( routes => { routes.MapRoute( name: "api", template: "api/{controller}/{action}/{id?}"); // ## commented out since I don't want to use MVC to serve my index. // routes.MapRoute( // name:"spa-fallback", // template: "{*anything}", // defaults: new { controller = "Home", action = "Index" }); }); // ## this serves my index.html from the wwwroot folder when // ## a route not containing a file extension is not handled by MVC. // ## If the route contains a ".", a 404 will be returned instead. app.MapWhen(context => context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value), branch => { branch.Use((context, next) => { context.Request.Path = new PathString("/index.html"); Console.WriteLine("Path changed to:" + context.Request.Path.Value); return next();}); branch.UseStaticFiles(); }); }

更多推荐

MVC 6路由,SPA后备+ 404错误页面

本文发布于:2023-11-12 13:47:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581670.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   错误   页面   MVC   SPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!