当前位置:编程学习 > C#/ASP.NET >>

MVC路由配置

当url不满足我配置的路由规则的时候 会出错 如何配置路由 跳转到一个404页面 mvc 路由 --------------------编程问答--------------------
引用 楼主 u012880745 的回复:
当url不满足我配置的路由规则的时候 会出错 如何配置路由 跳转到一个404页面

这个是需要在路由器里设置吧 --------------------编程问答-------------------- 都已经不匹配你的路由了,你让人家怎么用你的路由跳404?
路由不匹配会引发异常,可以在Global中配置application_error来跳转你指定的404页面。


        protected void Application_Error(object sender, EventArgs e)
        {
            Exception ex = Server.GetLastError();

            if (HttpContext.Current.Server.GetLastError() is HttpRequestValidationException)
            {
                HttpContext.Current.Response.Redirect("~/NiMei/NiDaYe");
                HttpContext.Current.Server.ClearError();
            }
        }


或者在application_start中注册错误全局Filte

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            GlobalFilters.Filters.Add(new HandleErrorAttribute());
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }

在你的system.web里:
<customErrors mode="On" defaultRedirect="/NiMei/Error"/> --------------------编程问答-------------------- http://blog.csdn.net/afeiflyinsky/article/details/7622622 --------------------编程问答-------------------- 楼主Application_Error是一种处理方式,还有救是重写处理,如:
protected override void HandleUnknownAction(string actionName)
        {
            try { base.HandleUnknownAction(actionName); }
            catch { Response.Redirect("404.html"); }
        }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,