IIS显示服务器错误而不是自定义错误

时间:2021-04-21 17:01:02

I'm using MVC 5 and I'm handling my errors with a custom views for errors such as (404, 403.. etc) It works fine on my local IIS but when I publish on a staging server it shows the IIS server error messages regarding these error codes.

我正在使用MVC 5并且我正在使用自定义视图处理我的错误,例如(404,403等)它在我的本地IIS上工作正常但是当我在登台服务器上发布时它显示IIS服务器错误有关这些错误代码的消息。

It's showing this message:

它显示了这条消息:

IIS显示服务器错误而不是自定义错误

instead of:

IIS显示服务器错误而不是自定义错误

I have modified the web.config for <customErrors mode="Off" />

我已经为 修改了web.config

Global.asax

        if ((Context.Server.GetLastError() is UnauthorizedAccessException))
        {
            log.LogError(Context.Server.GetLastError().Message, Context.Server.GetLastError());
            customErrorPage = @"~/Error/?id=403"; //security
        }
        else if ((Context.Server.GetLastError() is HttpException) && (((HttpException)Context.Server.GetLastError()).GetHttpCode() == 404))
        {
            //** Handle 404 error and response code
            log.LogError("404", Context.Server.GetLastError());
            customErrorPage = @"~/Error/?id=404";
        }
        else
        {
            log.LogError(Context.Server.GetLastError().Message, Context.Server.GetLastError());
            customErrorPage = @"~/Error";
        }

        if (ConfigurationHelper.Common.ShowCustomErrorPage)
        {
            var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
            Response.Redirect(urlHelper.Content(customErrorPage), false);
            Server.ClearError();
        }

Error controller:

    public ActionResult Index(string id)
    {
        if (!string.IsNullOrEmpty(id) && id.Equals("404"))
        {
            Response.StatusCode = 404;
            return !Request.IsAjaxRequest() ? (ActionResult)View("404") : PartialView("404");
        }
        if (!string.IsNullOrEmpty(id) && id.ToLower().Equals("403"))
        {
            Response.StatusCode = 403;
            return !Request.IsAjaxRequest() ? (ActionResult)View("Security") : PartialView("Security");
        }
        return !Request.IsAjaxRequest() ? (ActionResult)View("Index") : PartialView("Index");
    }

What should I do in-order to show my custom error messages?

我该怎么做才能显示我的自定义错误消息?

2 个解决方案

#1


9  

Just add the following web.config configuration to pass through IIS default error handling behavior

只需添加以下web.config配置即可通过IIS默认错误处理行为

<configuration>
    <system.webServer>
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>

#2


1  

Try to use

尝试使用

Response.TrySkipIisCustomErrors = true;

at your controller. It's better than the

在你的控制器。它比

<httpErrors existingResponse="PassThrough" />

as the PassThrough can cause some results you don't always expect. For example read here how it will make custom error module return blank response if modules don’t set any text.

因为PassThrough会导致一些你并不总是期望的结果。例如,如果模块没有设置任何文本,请在此处阅读如何使自定义错误模块返回空白响应。

#1


9  

Just add the following web.config configuration to pass through IIS default error handling behavior

只需添加以下web.config配置即可通过IIS默认错误处理行为

<configuration>
    <system.webServer>
        <httpErrors existingResponse="PassThrough" />
    </system.webServer>
</configuration>

#2


1  

Try to use

尝试使用

Response.TrySkipIisCustomErrors = true;

at your controller. It's better than the

在你的控制器。它比

<httpErrors existingResponse="PassThrough" />

as the PassThrough can cause some results you don't always expect. For example read here how it will make custom error module return blank response if modules don’t set any text.

因为PassThrough会导致一些你并不总是期望的结果。例如,如果模块没有设置任何文本,请在此处阅读如何使自定义错误模块返回空白响应。