asp.net-mvc – IIS显示服务器错误而不是自定义错误

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

显示了这条消息:

代替:

修改了web.config for< customErrors mode =“Off”/>

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();
        }

错误控制器:

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");
    }

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

解决方法

只需添加以下web.config配置即可通过IIS错误处理行为
<configuration>
    <system.webServer>
        <httpErrors existingResponse="Passthrough" />
    </system.webServer>
</configuration>

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....