我如何赶上服务器错误的HTTP状态code到ASP.NET MVC的通用自定义错误观点 [英] How do I catch the http status code of server errors into a generic custom error view in ASP.NET MVC

查看:133
本文介绍了我如何赶上服务器错误的HTTP状态code到ASP.NET MVC的通用自定义错误观点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在ASP.NET MVC 4是实现一个通用自定义错误页。
我基本上建立了一个错误的布局,它定义了部分用于输出响应的HTTP状态code。

I am trying to implement a general custom error page in ASP.NET MVC 4. I basically set up an Error Layout, which defines a section for outputting the http status code of the response.

这是我希望我的错误,在继承了这种布局结束,并简单地补充说,来自它的模型,该模型被实例化,并通过在调用视图()控制器中的消息视图(命名为错误)我成立了处理自定义错误,在web.config。

The view that I want my errors to end up at inherits such layout, and simply adds a message that comes from its model, which was instantiated and passed in the call to View() in the Controller (named "Error") I set up to handle custom errors, in the web.config.

<customErrors defaultRedirect="/Error" mode="On"> 
</customErrors>

在自定义错误控制器

public class ErrorController : Controller
{
    public ActionResult Index()
    {
        return View(new CustomErrorViewModel(Response.Status));
    }
}

自定义的错误视图

@{
    Layout = "~/Views/Shared/_CustomErrorLayout.cshtml";
}

@using System.Web.Configuration;
@using System.Configuration;
@using MVC_Tests.Models;
@model CustomErrorViewModel

@section HttpCode { @Response.StatusCode }

@Model.Status

的布局是:

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title></title>
    <link href="~/Content/CustomError.css" rel="stylesheet" />
</head>
<body>
    <div id="divError">
        <h1>
            <span>Error &nbsp;</span>
            @RenderSection("HttpCode")
        </h1>
        <div id="divErrorMessage">
            <p>@RenderBody()</p>
        </div>
    </div>
</body>
</html>

我想处理的错误是一个简单的除法被零我添加到主页索引操作。请注意,我想要一个单一视图,以处理不同服务器的HTTP状态错误codeS(500家)。 我知道有每个不同的HTTP状态code的作品有不同的看法,我是一个单一视图后处理多个codeS。

The error I am trying to handle is a simple division by zero I added to the Home Index action. Note that I want a single view to handle different server http status error codes (500 family). I know having a different view for each distinct http status code works, I am after a single view to handle multiple codes.

自定义错误处理的工作,我在所需的视图结束 - 但 - 正如上面提到的,我预计输出错误的HTTP状态code。
相反,我总是显示200(OK),其中,根据我的理解和调试,是因为:

The custom error handling is working, I am ending in the desired view - but - as noted above, I expected to output the http status code of the error. Instead, I am always displaying 200 (OK), which, according to my understanding and debugging, happens because:

1 首先,经与零师磕磕绊绊,将引发异常。

1. First, upon stumbling with the division by zero, an exception is raised.

2 立即重定向发生时,由于我的web.config的指示来处理自定义的方式错误(因为没有特定的HTTP状态code在web.config中指定的,我天天看HTTP状态错误code在同一个控制器/视图(/错误)。什么是这里的关键是,重定向是请求。

2. Now a redirect takes place, due my web.config instructions to handle errors in a customized way (since no specific http status code was specified in the web.config, I handle every http status error code in the same controller/view ("/Error"). What is crucial here is that the redirect is a new request.

3。第2步重定向发送我的错误控制器,它然后将我的观点,这是渲染。

3. The redirect from step 2 sends me to the Error Controller, and it then sends me to its view, which is rendered.

4。在渲染视图,HTTP状态code插入布局定义的部分,加入我的自定义消息,HTTP状态code部分的输出正如我所说的:200(OK)。为什么呢,如果一个500服务器端错误被强行抛出?因为,如果我没有错,观点是不同的请求管道内呈现时,重定向之一,并在重定向 - 发生任何错误 - 我结束了一个200(OK)应答

4. In rendering the view, the http status code inserted into the layout defined section, my custom message is added, and the output of the http status code part is as I said: 200 (OK). Why, if a 500 server side error was forcibly thrown ? Because, if I am not wrong, the view is rendered within a different request pipe, the redirect one, and in redirecting - no errors occur - and I end up with a 200 (OK) status response.

我去了后由迪诺·埃斯波西托关于asp.net的MVC处理错误,但我不希望添加的过滤器(onException的和属性的HandleError解决方案)的所有应用程序的控制器(我可以创建有这样一个过滤器新基地控制器,并让所有的人继​​承它,但也需要改变所有控制器类)。他提到(处理Global.asax中的Application_Error事件)我不知道最后的方法确实达到这个目的 - 我有问题处理程序重定向的Response.Redirect语句,它的跳动能够设置自定义页面的灵活性在web.config(我仍然可以重定向到在web.config中定义的路线,但不知这是没有得到的东西,看起来那么简单太麻烦了)。

I went over this post by Dino Esposito about handling errors in asp.net mvc, but I do not want to add filters (OnException and HandleError attribute solutions) to all the app's controllers (I could create a new base controller that has such filter, and make all the others inherit from it, but that also requires changing all controller classes). The last approach he mentions (handling global.asax's Application_Error event) I am not sure does serve this purpose - I have to issue a redirect "Response.Redirect" statement in the handler, which beats the flexibility of being able to set the custom pages in the web.config (I could still redirect to a route defined in the web.config, but I wonder if this is not getting too cumbersome for something that looks so simple).

什么是捕捉任何服务器错误响应的HTTP状态code到我的自定义错误观点的最好方法是什么?

推荐答案

如果您正在使用IIS7 +我会禁用的customErrors像这样:

If you're using IIS7+ I'd disable "customerrors" like so:

 <customErrors mode="Off" />

和使用httpErrors像这样:

and use httpErrors like so:

<system.webServer>
 <httpErrors errorMode="DetailedLocalOnly" existingResponse="Replace" defaultPath="/error">
     <remove statusCode="404" />
     <error statusCode="404" path="/error/notfound" responseMode="ExecuteURL" />
 </httpErrors>
....

这可以让你得到一个非URL变化用在你的控制器以下404状态code显示错误页面:

This allows you to get a non-url changing error page displaying with a 404 status code using the following in your controller:

public class ErrorController : Controller
{
public ActionResult notfound()
    {
        Response.TrySkipIisCustomErrors = true;
        Response.StatusCode = (int)HttpStatusCode.NotFound;
        return View("OneErrorViewToRuleThemAll");
    }
}

PS。 errorMode需要更改为自定义,看看它在本地....取决于您的本地设置(例如,如果你使用IISEx preSS),你可能不会看到正确的错误,直到您上传到现场服务器

ps. "errorMode" needs to change to "Custom" to see it locally....depending on your local setup (i.e. if you're using IISExpress) you might not see the proper error until you upload to a live server.

PPS。你可以有单一视图OneErrorViewToRuleThemAll来处理任何状态codeS的屏幕上输出你决定如何处理这种方式。每个ErrorController行动将结束这种观点。

pps. you could have the single view "OneErrorViewToRuleThemAll" to handle the on-screen output of whatever status codes you decide to handle this way. Each ErrorController Action would end up on this view.

这篇关于我如何赶上服务器错误的HTTP状态code到ASP.NET MVC的通用自定义错误观点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆