2013-10-21 33 views
4

我有一個.NET MVC應用程序,我無法使用IIS 8在服務器上處理自定義錯誤頁面。在整個應用程序中,我捕捉並拋出異常並在我的錯誤頁面上顯示一條消息,並根據他們的違規行爲進行定製。當在調試中通過VS運行應用程序時,以及在IIS(6.1)中配置本地主機上的站點時,這在本地都很有用。.Net MVC自定義錯誤頁面在IIS8中不工作

然後我將它部署到安裝了IIS 8的服務器上。最初我得到了令人討厭的默認500錯誤頁面:default 500 error page http://img202.imageshack.us/img202/1352/b8gr.png

經過一點研究,我發現我可以將以下內容添加到web.config中,至少可以讓我訪問我友好的錯誤頁面,儘管沒有自定義文本:

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); 
     } 
    } 
} 

任何想法:

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

我用下面的過濾器實現自定義錯誤消息?

回答

4

我有同樣的問題,實際上管理,以使其通過改變我的配置工作:

<system.web> 
    <customErrors mode="RemoteOnly"> 
     <error statusCode="404" redirect="/Error/E404" /> 
    </customErrors> 
</system.web> 

請更改/錯誤/ E404到您的自定義錯誤路徑。然後,我還補充道:

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

我也沒有通過Plesk一些變化,但我相信,httpErrors線使得它到底正常工作。

相關問題