C# Asp.Net Mvc路由-错误路由
我正在尝试路由应用程序中找不到的错误页面,如果任何路由错误,则显示错误路由 下面是我的默认路线C# Asp.Net Mvc路由-错误路由,c#,asp.net,asp.net-mvc,asp.net-mvc-routing,C#,Asp.net,Asp.net Mvc,Asp.net Mvc Routing,我正在尝试路由应用程序中找不到的错误页面,如果任何路由错误,则显示错误路由 下面是我的默认路线 routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); routes.MapRoute( "NotFound",
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
"NotFound",
"{*.}",
new { controller = "Error", action = "PageNotFound" }
);
将其添加到web.config文件中
<system.webServer>
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="404" />
<error statusCode="404" responseMode="ExecuteURL" path="/Error" />
<remove statusCode="500" />
<error statusCode="500" responseMode="ExecuteURL" path="/Error" />
</httpErrors>
<modules>
<remove name="FormsAuthentication" />
</modules>
点(.)将导致冲突。使用alpha字符作为参数名称
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
//Catch-All InValid (NotFound) Routes
routes.MapRoute(
name: "NotFound",
url: "{*url}",
defaults: new { controller = "Error", action = "PageNotFound" }
);
它还允许您在操作中获取参数
public class ErrorController : Controller {
public ActionResult PageNotFound(string url) { ... }
}
以防您想对该值执行任何其他操作。ie日志记录、审核…等感谢重播,但它不起作用,当我运行应用程序时,我希望每次打开我的默认路径主页,如果我打开其他路径,如userlist或edit Page,则转到Page404查看