asp.net-mvc web.config 中的 <customErrors> 在哪里用于 MVC 应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16251134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where does <customErrors> in web.config go for MVC applications?
提问by Hosea146
I'm attempting to implement custom error handling in my MVC 4 app. I'm not certain where in my web.config the <customErrors>is supposed to go, and the general information I need to include in it.
我正在尝试在我的 MVC 4 应用程序中实现自定义错误处理。我不确定我的 web.config<customErrors>应该去哪里,以及我需要在其中包含的一般信息。
回答by Ant P
<customErrors>goes inside <system.web>:
<customErrors>进去<system.web>:
<configuration>
<system.web>
<customErrors mode="RemoteOnly">
<error statusCode="500"
redirect="~/Error/InternalServer" />
<error statusCode="404"
redirect="~/Error/NotFound" />
</customErrors>
</system.web>
</configuration>
Modify values of the redirectattributes according to your routes. You can also implement a catch-all redirect by adding a defaultRedirectattribute to the customErrorselement. See this MSDN articlefor more information.
redirect根据您的路由修改属性值。您还可以通过向元素添加defaultRedirect属性来实现全能重定向customErrors。有关更多信息,请参阅此 MSDN 文章。
回答by Md Jahangir Alam
In web.config file under root directory
在根目录下的 web.config 文件中
<system.web>
<customErrors mode="On">
</customErrors>
Create a shared folder under view folder, and create a view under shared folder for showing Error message
in action use that [HandleError] like
在视图文件夹下创建一个共享文件夹,并在共享文件夹下创建一个视图以显示错误信息
在行动中使用 [HandleError] 喜欢
[HandleError]
public ActionResult Errors()
{
throw new Exception();
}

