如何将服务器错误的 http 状态代码捕获到 ASP.NET MVC 中的通用自定义错误视图中 [英] How do I catch the http status code of server errors into a generic custom error view in ASP.NET MVC
本文介绍了如何将服务器错误的 http 状态代码捕获到 ASP.NET MVC 中的通用自定义错误视图中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在 ASP.NET MVC 4 中实现一个通用的自定义错误页面.我基本上设置了一个错误布局,它定义了一个部分,用于输出响应的 http 状态码.
我希望我的错误最终出现的视图继承了这样的布局,并简单地添加了来自其模型的消息,该消息被实例化并在控制器中对 View() 的调用中传递(名为错误")我在 web.config 中设置为处理自定义错误.
</customErrors>
自定义错误控制器:
公共类 ErrorController : 控制器{公共 ActionResult 索引(){返回视图(新的 CustomErrorViewModel(Response.Status));}}
自定义错误视图:
@{布局 = "~/Views/Shared/_CustomErrorLayout.cshtml";}@using System.Web.Configuration;@使用系统配置;@using MVC_Tests.Models;@model CustomErrorViewModel@section HttpCode { @Response.StatusCode }@Model.Status
布局是:
<头><meta name="viewport" content="width=device-width"/><title></title><link href="~/Content/CustomError.css" rel="stylesheet"/>头部><身体><div id="divError"><h1><span>错误 </span>@RenderSection("HttpCode")<div id="divErrorMessage"><p>@RenderBody()</p>