C# 在MVC中显示常规错误页面
我有一个MVC应用程序,当出现错误时,我需要向用户显示一个特定的视图 因此,如果错误是code=200、code=404、code=500或任何其他错误,我只需显示位于C# 在MVC中显示常规错误页面,c#,xml,asp.net-mvc,asp.net-mvc-4,C#,Xml,Asp.net Mvc,Asp.net Mvc 4,我有一个MVC应用程序,当出现错误时,我需要向用户显示一个特定的视图 因此,如果错误是code=200、code=404、code=500或任何其他错误,我只需显示位于~/Views/Shared/error.cshtml中的错误视图,并显示一条常规消息 那么我的问题是:在global.asax中添加这个就足够了吗 public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(
~/Views/Shared/error.cshtml
中的错误视图,并显示一条常规消息
那么我的问题是:在global.asax中添加这个就足够了吗
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
或者我还需要这样修改我的web.config:
<customErrors mode="RemoteOnly">
<error statusCode="404" redirect="~/Views/Shared/Error.cshtml"/>
<error statusCode="500" redirect="~/Views/Shared/Error.cshtml"/>
</customErrors>
只有一个选项是必需的,或者我必须添加这两个选项吗
那么我的问题是:在global.asax中添加这个就足够了吗
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
否,因为HandleErrorAttribute
将只捕获错误代码500(内部服务器错误)
如果您想在500之外捕获404(未找到页面),最好的方法是使用httpErrors
,因为它们也将捕获与您定义的任何路由都不匹配的404
自定义错误将只捕获与路由匹配的404。例如,404来自:
不会被自定义错误捕获(假定不存在与URL匹配的路由)。来自的404将被自定义错误捕获
我在web.configsystem.webserver
部分中设置了类似的httpErrors
:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="400" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="400" path="/Error/BadRequest" responseMode="ExecuteURL"/>
<error statusCode="403" path="/Error/NotAuthorized" responseMode="ExecuteURL" />
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
<error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
那么我的问题是:在global.asax中添加这个就足够了吗
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
否,因为HandleErrorAttribute
将只捕获错误代码500(内部服务器错误)
如果您想在500之外捕获404(未找到页面),最好的方法是使用httpErrors
,因为它们也将捕获与您定义的任何路由都不匹配的404
自定义错误将只捕获与路由匹配的404。例如,404来自:
不会被自定义错误捕获(假定不存在与URL匹配的路由)。来自的404将被自定义错误捕获
我在web.configsystem.webserver
部分中设置了类似的httpErrors
:
<httpErrors errorMode="Custom" existingResponse="Replace">
<remove statusCode="400" subStatusCode="-1" />
<remove statusCode="403" subStatusCode="-1" />
<remove statusCode="404" subStatusCode="-1" />
<remove statusCode="500" subStatusCode="-1" />
<error statusCode="400" path="/Error/BadRequest" responseMode="ExecuteURL"/>
<error statusCode="403" path="/Error/NotAuthorized" responseMode="ExecuteURL" />
<error statusCode="404" path="/Error/NotFound" responseMode="ExecuteURL" />
<error statusCode="500" path="/Error" responseMode="ExecuteURL" />
</httpErrors>
您可以在基本控件中创建自定义过滤器,然后在每个控制器中应用该过滤器,因此当发生任何错误时,它将在公共逻辑中捕获并呈现定义的视图/html 请看下面的线。我已经描述了如何处理属性中的服务器端错误和客户端错误
您可以在基本控件中创建自定义过滤器,然后在每个控制器中应用该过滤器,因此当发生任何错误时,它将在公共逻辑中捕获并呈现定义的视图/html 请看下面的线。我已经描述了如何处理属性中的服务器端错误和客户端错误
200
不是错误代码。另外,如果您不想重定向到.cshtml
文件,则需要重定向到返回视图的操作
。因此我需要的是RegisterGlobalFilters选项?否,您需要创建该控制器的错误控制器
和索引
操作。然后将web.config更改为重定向到~/Error
。查看示例。为什么不使用registerGlobalFilter并在web.config中定义customErrors mode=“on”…这样,我想它将显示位于共享文件夹200
中的默认错误页不是错误代码。此外,您不想重定向到.cshtml
文件,您需要重定向到返回视图的操作
。因此,RegisterGlobalFilters选项是我需要的选项?不,您需要创建该控制器的错误控制器
和索引
操作。然后将web.config更改为重定向到~/Error
。请看示例。为什么不使用registerGlobalFilter并在web.config中定义customErrors mode=“on”…这样,我想它将显示位于共享文件夹中的默认错误页面