Asp.net mvc 4 自定义401错误在本地工作但不在服务器上

Asp.net mvc 4 自定义401错误在本地工作但不在服务器上,asp.net-mvc-4,c#-4.0,Asp.net Mvc 4,C# 4.0,我已经在my global.asax.cs中定义了自定义错误页,并且可以让它们在IIS Express和VS 2013中本地工作,但是当我发布到运行IIS 7的服务器时,401和403错误不会显示自定义错误页,而是返回一个500而不是自定义500页。我在这里尝试了很多解决方案,但都没有成功。有人有什么想法吗 Web.Config部分: <system.web> <customErrors mode="On" defaultRedirect="~/Error">

我已经在my global.asax.cs中定义了自定义错误页,并且可以让它们在IIS Express和VS 2013中本地工作,但是当我发布到运行IIS 7的服务器时,401和403错误不会显示自定义错误页,而是返回一个500而不是自定义500页。我在这里尝试了很多解决方案,但都没有成功。有人有什么想法吗

Web.Config部分:

  <system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
    </customErrors>
  </system.web>
  <system.webServer>
    <httpErrors existingResponse="PassThrough" />
  </system.webServer>
   protected void Application_Error()
    {
        string descriptor = null;
        string errMsg = null;
        string additionalInfo = null;
        string title = null;
        Exception lastError = null;

        Exception ex = null;
        if (HttpContext.Current.Server.GetLastError() != null)
        {
            ex = HttpContext.Current.Server.GetLastError().GetBaseException();
            if (ex.GetType() == typeof (HttpException))
            {
                HttpException httpException = (HttpException) ex;
                switch (httpException.GetHttpCode())
                {
                    case 404:
                        title = "Not Found";
                        descriptor = "Page Not Found";
                        errMsg =
                            "The page you requested could not be found, either contact your webmaster or try again. Use your browsers Back button to navigate to the page you have prevously come from.";
                        additionalInfo =
                            "We are working hard to correct this issue. Please wait a few moments and try your search again.";
                        lastError = new Exception(errMsg);
                        break;
                    case 500:
                        title = "Server Error";
                        descriptor = "Oooops, Something went wrong!";
                        errMsg = "You have experienced a technical error. We apologize.";
                        additionalInfo =
                            "We are working hard to correct this issue. Please wait a few moments and try your search again.";
                        lastError = new Exception(errMsg);
                        break;
                }

                CallErrorController(descriptor, additionalInfo, title, lastError, httpException.GetHttpCode());
            }
        }
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        if (Response.StatusCode == 401 || Response.StatusCode == 403)
        {
            Response.ClearContent();
            string additionalInfo = null;
            string title = null;
            const string errMsg =
                @"You have attempted to access a resource for which you do not have the proper authorization or which is not available from your location. 
                                    If you received this message by clicking on a link on this website, please report it to the webmaster. Use your browsers Back 
                                    button to navigate to the page you have previously come from.";

            const string descriptor = "Access is Denied";
            title = descriptor;
            Exception lastError = new Exception(errMsg);

            CallErrorController(descriptor, additionalInfo, title, lastError, Response.StatusCode);
        }
    }

    private void CallErrorController(string descriptor, string additionalInfo, string title, Exception lastError, int statusCode)
    {
        Server.ClearError();
        Response.TrySkipIisCustomErrors = true;
        HttpContextWrapper contextWrapper = new HttpContextWrapper(this.Context);

        RouteData routeData = new RouteData();
        routeData.Values.Add("controller", "Error");
        routeData.Values.Add("action", "Index");
        routeData.Values.Add("statusCode", statusCode);
        routeData.Values.Add("exception", lastError);
        routeData.Values.Add("descriptor", descriptor);
        routeData.Values.Add("additionalInfo", additionalInfo);
        routeData.Values.Add("title", title);
        routeData.Values.Add("isAjaxRequet", contextWrapper.Request.IsAjaxRequest());

        IController controller = new ErrorController();

        RequestContext requestContext = new RequestContext(contextWrapper, routeData);

        controller.Execute(requestContext);
        Response.End();
    }
错误控制器:

public class ErrorController : Controller
{
    public ActionResult Index(int statusCode, Exception exception, string descriptor, string additionalInfo, string title, bool isAjaxRequet)
    {
        if (!isAjaxRequet)
        {
            ErrorModel model = new ErrorModel { HttpStatusCode = statusCode, Exception = exception, Descriptor = descriptor, AdditionalInfo = additionalInfo, Title = title};
            return View("Error", model);
        }
        else
        {
            // Otherwise, if it was an AJAX request, return an anon type with the message from the exception
            var errorObjet = new { message = exception.Message };
            return Json(errorObjet, JsonRequestBehavior.AllowGet);
        }
    }
}
本地运行:

从服务器运行:

尝试将其添加到web.config

<configuration>
    ... //all the other gubbins that goes in your web config
    <system.webServer>
        <httpErrors errorMode="Detailed" />
    </system.webServer>
</configuration>

或者通过iis adamin模块打开详细错误,该模块会导致500个错误。该站点根本无法启动。是否将其放入根配置对象中?是的,我将其放入其中尝试按照此处的步骤使用ui打开:在删除web.config中的自定义错误设置后,我能够通过GUI设置errorMode=Detailed。它返回了详细信息,但没有返回401-403错误的自定义页面