.Net MVC自定义错误页面在IIS8中不起作用 [英] .Net MVC custom error page not working in IIS8

查看:188
本文介绍了.Net MVC自定义错误页面在IIS8中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个.NET MVC应用程序,我无法使用IIS 8在服务器上运行自定义错误页面。在我的应用程序中,我捕获并抛出异常,并在我的错误页面上显示一条消息被定制为违规。这通过VS在调试中运行应用程序,当我在IIS(6.1)中的本地主机上配置一个站点时,这一切都在本地运行。



然后我将其部署到安装了IIS 8的服务器。最初我得到了令人讨厌的默认500错误页面:默认500错误页面http:// img202.imageshack.us/img202/1352/b8gr.png



做了一点研究后,我发现我可以添加以下内容到web.config至少会让我的友好的错误页面,尽管没有自定义的文本:

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

我使用以下过滤器完成自定义错误消息:



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;

//显示自定义异常的特定错误消息,否则通用消息
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 =处理您的请求时出错。
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);
}
}
}

任何想法?

解决方案

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

 < 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.

这篇关于.Net MVC自定义错误页面在IIS8中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆