在我的web.config中,我有:
<system.web> <customErrors mode="On" defaultRedirect="Error.cshtml" /> </system.web>
在Views / Shared / Error.cshtml中,我有:
@model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = "Error"; } <h2> Sorry,an error occurred while processing your request. </h2>
如果我将无效的URL /路由放入浏览器中,我得到:
Server Error in ‘/’ Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed,had its name changed,or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Error.cshtml
为什么找不到Error.cshtml?该文件绝对在视图/共享.
解决方法
defaultRedirect不会直接转到视图.您的defaultRedirect看起来像一个无法处理的剃刀视图文件.例如:从哪里得到模型?在配置文件中不能指定,也不能指定,因此无法处理视图.
如果您想在MVC中有更多动态错误页面,您可能需要阅读custom error pages and error handling in MVC 3