问题描述
我使用Visual Studio 2012与MVC 4框架,并希望看到共享文件夹的友好错误页面,如果未处理的异常。 (我知道,作为一个develepor我需要看到错误的详细信息,但我只是想测试这个设置))
到目前为止,我知道在web.config的system.web部分添加以下代码应该做的伎俩
< customErrors mode =On/>
不幸的是,我的友好的错误页面是这样的:
运行时错误说明:
服务器上发生应用程序错误。此应用程序的当前自定义错误设置阻止
查看应用程序错误的详细信息。
详细信息:要在本地服务器机器上查看此特定错误消息的详细信息为
,请在 web.config配置文件位于当前Web应用程序的根
目录中。该标签
应该将其mode属性设置为RemoteOnly。要启用
,请在远程计算机上查看详细信息,请将mode设置为
Off。
<! - Web.Config配置文件 - >
< configuration>
< system.web>
< customErrors mode =RemoteOnly/>
< /system.web> < /结构>
<! - Web.Config配置文件 - >
< configuration>
< system.web>
< customErrors mode =OndefaultRedirect =mycustompage.htm/>
< /system.web> < /结构>
我会喜欢按照描述进行操作,而不是像在向HomeController添加操作,设置路由或某些东西等一些解决方法。
这个应该起作用的一个参考是:
UPDATE (link changed):
http://pluralsight.com/training/Player?author=scott-allen&name=mvc4-building-m2-controllers&mode=live&clip=0&course=mvc4-building
(On top click on the thin line that states "Controller in ASP.NET MVC 4", then click on "Action Filters" and skip to minute 5:00)
Does anybody have a clue why I cannot see the friendly error page with the setting mentioned above?
Thanks in advance!
Here is step by step guide to doing this,
To start with, first set the "customErrors’s" mode property to ‘on’. (This specifies that custom errors are enabled.)
1. Code from Web.config
<system.web>
<customErrors mode="On"></customErrors>
</system.web>
Below is my controller code, I have specified the HandleError attribute on the controller class. Also to generate an error I am throwing an exception from the Index action.
2. Code from 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();
}
}
As you can see in the screenshot below, I have a Error.cshtml view defined inside the Shared folder of the Views folder.
3. Solution Explorer
Note that the Error.cshtml view (shown below) uses a model of ‘System.Web.Mvc.HandleErrorInfo’
4. Code from Error.cshtml
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "Error";
}
<h2>
Sorry, an error occurred while processing your request.
</h2>
On running the project, instead of getting the yellow death screen now I get the following output.
Taken from this article
Update :
Try this out by creating a new project, this should and does work. I suspect that your Error.cshtml
file may not have model as @model System.Web.Mvc.HandleErrorInfo
or you may be having multiple Error.cshtml pages in different view folders.
这篇关于自定义错误页面不显示即使我设置CustomError =“开”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!