简体   繁体   English

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

[英].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. 我有一个.NET MVC应用程序,我在使用IIS 8的服务器上运行自定义错误页面时遇到问题。在我的应用程序中,我正在捕捉并正确地抛出异常并在我的错误页面上显示一条消息,该页面是自定义的他们的违规行为。 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). 当在调试中通过VS运行应用程序时以及在IIS(6.1)中的本地主机上配置站点时,这一切都在本地运行良好。

I then deployed it to a server with IIS 8 installed. 然后我将其部署到安装了IIS 8的服务器上。 Initially I was getting the nasty, default 500 error page: default 500 error page http://img202.imageshack.us/img202/1352/b8gr.png 最初我得到了令人讨厌的默认500错误页面: 默认500错误页面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: 在做了一点研究之后,我发现我可以将以下内容添加到web.config中,至少会把我的友好错误页面给我,虽然没有自定义文本:

<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. 请将/ Error / E404更改为您的自定义错误路径。 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. 我也通过Plesk进行了一些更改,但我相信httpErrors行最终能够正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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