到目前为止,我知道在web.config的system.web部分添加以下代码应该可以解决问题
<customErrors mode="On" />
不幸的是,我得到了这个:
Server Error in ‘/’ Application.
Runtime Error Description: An application error occurred on the
server. The current custom error settings for this application prevent
the details of the application error from being viewed.Details: To enable the details of this specific error message to be
viewable on the local server machine,please create a
tag within a “web.config” configuration file located in the root
directory of the current web application. This tag
should then have its “mode” attribute set to “RemoteOnly”. To enable
the details to be viewable on remote machines,please set “mode” to
“Off”.
<!-- Web.Config Configuration File --> <configuration> <system.web> <customErrors mode="RemoteOnly"/> </system.web> </configuration>
Notes: The current error page you are seeing can be replaced by a
custom error page by modifying the “defaultRedirect” attribute of the
application’s configuration tag to point to a custom
error page URL.
<!-- Web.Config Configuration File --> <configuration> <system.web> <customErrors mode="On" defaultRedirect="mycustompage.htm"/> </system.web> </configuration>
我想按照描述执行此操作,而不是使用向HomeController添加操作,设置路由或其他内容的解决方法.
这应该起作用的参考如下:
http://pluralsight.com/training/players/PSODPlayer?author=scott-allen&name=mvc3-building-controllers&mode=live&clip=0&course=aspdotnet-mvc3-intro
(在左侧选择“动作过滤器”一章并跳至分钟3:30)
更新(链接已更改):
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building
(在顶部单击描述“ASP.NET MVC 4中的Controller”的细线,然后单击“Action Filters”并跳到分钟5:00)
解决方法
首先,首先将“customErrors”模式属性设置为“on”. (这指定启用自定义错误.)
1.来自Web.config的代码
<system.web> <customErrors mode="On"></customErrors> </system.web>
下面是我的控制器代码,我在控制器类上指定了HandleError属性.同样为了生成错误,我从Index操作中抛出异常.
2.来自HomeController.cs的代码
[HandleError] public class HomeController : Controller { public ActionResult Index() { throw new Exception("Error Occured !"); ViewBag.Message = "Welcome to ASP.NET MVC!"; return View(); } public ActionResult About() { return View(); } }
正如您在下面的屏幕截图中看到的,我在Views文件夹的Shared文件夹中定义了一个Error.cshtml视图.
3.解决方案Explorer
请注意,Error.cshtml视图(如下所示)使用’System.Web.Mvc.HandleErrorInfo’模型
4.来自Error.cshtml的代码
@model System.Web.Mvc.HandleErrorInfo @{ ViewBag.Title = "Error"; } <h2> Sorry,an error occurred while processing your request. </h2>
在运行项目时,我现在获得以下输出,而不是获得黄色死亡屏幕.
更新:
通过创建一个新项目来尝试这个,这应该并且确实有效.我怀疑您的Error.cshtml文件可能没有@model System.Web.Mvc.HandleErrorInfo的模型,或者您可能在不同的视图文件夹中有多个Error.cshtml页面.