简体   繁体   中英

.Net MVC custom error page not working in IIS8

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM