.Net MVC custom error page not working in IIS8

别等时光非礼了梦想. 提交于 2019-12-10 09:23:28

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/19502689/net-mvc-custom-error-page-not-working-in-iis8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!