2011-04-21 97 views
6

我創建了正確的模型類型,並不明白它來自何處。有任何想法嗎?這裏System.Web.Mvc.HandleErrorInfo錯誤的模型類型

 
public class ExtendendHandleErrorAttribute : HandleErrorAttribute 
    { 
     public override void OnException(ExceptionContext filterContext) 
     {         
      LogErrors(filterContext); 
      try 
      { 
       base.OnException(filterContext); 

       var typedResult = filterContext.Result as ViewResult; 
       if (typedResult != null) 
       { 
        var tmpModel = typedResult.ViewData.Model; 
        typedResult.ViewData = filterContext.Controller.ViewData; 
        typedResult.ViewData.Model = CreateModel(filterContext); 
        filterContext.Result = typedResult; 
       } 
      } 
      catch(Exception ex) 
      { 
       new LogManager().Log("ExtendendHandleErrorAttribute error", ex); 
      } 
     } 

有趣的是,我創建ErrorCodeModel:

 
System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Web.Mvc.HandleErrorInfo', 
but this dictionary requires a model item of type 'BusinessLogic.Models.ErrorCodeModel'. 
    at System.Web.Mvc.ViewDataDictionary`1.SetModel(Object value) 
    at System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary dictionary) 
    at System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) 
    at System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) 
    at System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) 
    at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) 
    at System.Web.Mvc.Controller.ExecuteCore() 
    at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) 
    at System.Web.Mvc.MvcHandler.c__DisplayClass6.c__DisplayClassb.b__5() 
    at System.Web.Mvc.Async.AsyncResultWrapper.c__DisplayClass1.b__0() 
    at System.Web.Mvc.MvcHandler.c__DisplayClasse.b__d() 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
中的global.asax.cs

我註冊一個自定義屬性:

 
     public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
     { 
      filters.Add(new Controllers.ExtendendHandleErrorAttribute()); 
     } 

的定義是這樣的。

 
     private ErrorCodeModel CreateModel(ExceptionContext filterContext) 
     { 
      var model = new ErrorCodeModel(); 

      if (filterContext.HttpContext.Session != null) 
      { 
       var session = filterContext.HttpContext.Session; 

       model.SessionId = session.SessionID; 
       StateHandler stateHandler = new StateHandler(session); 
       model.FapiErrorCode = stateHandler.CustomErrorCode.ToString();    
       try 
       {      
        model.GlobalData = new GlobalDataBuilder(stateHandler).Build(); 
        model.ErrorMessage = model.GlobalData.ErrorText.TechnicalError; 
       } 
       catch { } 
      } 

      return model; 
     } 

我的web.config

 
<customErrors mode="Off" defaultRedirect="Error"> 
     <error statusCode="404" redirect="Error/FileNotFound" /> 
    </customErrors> 
+0

你能解釋bri呃,你的情景,你試圖達到什麼目的,你爲了達到目標而做了什麼,什麼不起作用?那麼,最後一部分你已經顯示了錯誤信息。 – 2011-04-21 09:31:32

+0

更新的介紹:-) – NickD 2011-04-21 10:10:29

回答

3

~/Views/Shared/Error.cshtml替換第一行:

@model System.Web.Mvc.HandleErrorInfo 

@model BusinessLogic.Models.ErrorCodeModel 
+0

它已經使用@model BusinessLogic.Models.ErrorCodeModel – NickD 2011-04-21 14:22:46

+0

我的Error.cshtml中有一個自定義@model,我仍然得到錯誤。任何其他想法? – 2012-04-26 20:03:48

3

當你使用一個控制器或行動上[HandleError]屬性,拋出的異常,通過你的web.config描述將導致重定向到自定義錯誤頁。默認情況下,錯誤使用HandleErrorInfo模型轉到~\Views\Shared\Error.cshtml

的web.config部分,可以是這樣的:

<system.web> 
    <customErrors mode="On" defaultRedirect="Error.aspx"> 
    <error statusCode="403" redirect="NoAccess.htm"/> 
    <error statusCode="404" redirect="FileNotFound.htm"/> 
    </customErrors> 
</system.web> 
+0

是的,這是正確的。請閱讀我的更新說明。 – NickD 2011-04-21 10:12:43