且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

.Net MVC自定义错误页面在IIS8中不起作用

更新时间:2023-02-25 21:32:57

我有同样的问题,实际上通过将配置更改为:

 < system.web> 
< customErrors mode =RemoteOnly>
< error statusCode =404redirect =/ Error / E404/>
< / customErrors>
< /system.web>

请将/错误/ E404更改为自定义错误路径。然后我还添加了:

 < system.webServer> 
< httpErrors existingResponse =PassThrougherrorMode =Detailed>< / httpErrors>
< /system.webServer>

我也通过Plesk进行了一些更改,但我相信httpErrors行使其最终正常工作。


I have an .NET MVC app which I'm having trouble getting my custom error pages working on a server with IIS 8. Throughout my application, I'm catching and throwing exceptions appropriately and displaying an message on my error page which is customized to their infraction. This is all working great locally when running the app through VS in debug and also when I config a site on my localhost in IIS (6.1).

I then deployed it to a server with IIS 8 installed. Initially I was getting the nasty, default 500 error page: default 500 error page http://img202.imageshack.us/img202/1352/b8gr.png

After doing a little bit of research, I found that I could adding the following to the web.config would at least get me my friendly error page, albeit without customized text:

<system.webServer>
    <httpErrors errorMode="Detailed" />
</system.webServer>

I'm accomplishing the custom error message using the following filter:

public class GlobalExceptionFilter : FilterAttribute, IExceptionFilter
{
    public void OnException(ExceptionContext filterContext)
    {
        if (filterContext.HttpContext.IsCustomErrorEnabled && !filterContext.ExceptionHandled)
        {
            filterContext.ExceptionHandled = true;
            filterContext.HttpContext.Response.StatusCode = 500;
            string controllerName = (string)filterContext.RouteData.Values["controller"];
            string actionName = (string)filterContext.RouteData.Values["action"];
            HandleErrorInfo info = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

            IControllerFactory factory = ControllerBuilder.Current.GetControllerFactory();
            ErrorController newController = factory.CreateController(filterContext.RequestContext, "Error") as ErrorController;
            filterContext.RouteData.Values["controller"] = "Error";
            filterContext.Controller = newController;

            //Display specific error message for custom exception, otherwise generic message
            var model = new ErrorViewModel { Exception = info.Exception };
            if (info.Exception.GetType() == typeof(RchiveException) || info.Exception.GetType().IsSubclassOf(typeof(RchiveException)))
                model.Message = info.Exception.Message;
            else
                model.Message = "An error occurred processing your request.";
            filterContext.Controller.ViewData = new ViewDataDictionary(model);

            string actionToCall = "Index";
            if (filterContext.HttpContext.Request.IsAjaxRequest())
                actionToCall = "IndexAjax";

            filterContext.RouteData.Values["action"] = actionToCall;
            newController.ActionInvoker.InvokeAction(filterContext, actionToCall);
        }
    }
}

Any ideas?

I had the same issue and actually managed to make it work by changing my configuration to:

<system.web>
    <customErrors mode="RemoteOnly">
      <error statusCode="404" redirect="/Error/E404" />
    </customErrors>
</system.web>

Please change /Error/E404 to your custom error path. Then I also added:

  <system.webServer>
    <httpErrors existingResponse="PassThrough" errorMode="Detailed"></httpErrors>
  </system.webServer>

I also did some changes through Plesk but I believe that the httpErrors line made it work properly in the end.