Asp.Net Mvc路由 - 错误路由
问题描述:
我想路由错误页面找不到我的应用程序,如果任何路线是错误的,然后显示错误路线。Asp.Net Mvc路由 - 错误路由
下面
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>
答
点(。)会导致冲突。使用字母字符参数名称,
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) { ... }
}
的情况下,你想要做什么都与价值。即记录,审计...等
谢谢重播,但它不工作,当我运行应用程序,然后我希望每次打开我的默认路径主页,如果我打开其他路径像用户列表或编辑页面,然后它会去Page404视图 –