. Custom MVC Error Page MVC not working in IIS8

I have a .NET MVC application with which I have problems getting my own error pages working on a server with IIS 8. Throughout my application, I caught and threw exceptions properly and displayed a message on my error page, configured to violation. All this works fine when starting the application through VS during debugging, as well as when setting up the site on my local host in IIS (6.1).

Then I deployed it to the server with IIS 8 installed. Initially, I got an unpleasant default error page 500: default error page 500 http://img202.imageshack.us/img202/1352/b8gr.png

After doing a bit of research, I found that I can add the following to web.config, at least get me my page with a friendly error, although without an individual text:

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

I am executing my own 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?

+4
source share
1 answer

I had the same problem and managed to get it to 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 own error path. Then I added:

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

I also made some changes through Plesk, but I believe the httpErrors line made it work properly at the end.

+4
source

All Articles