【问题标题】:System.Web.Mvc.HandleErrorInfo wrong model typeSystem.Web.Mvc.HandleErrorInfo 模型类型错误
【发布时间】:2011-04-21 09:28:22
【问题描述】:

我创建了正确的模型类型,但不明白它的来源。有什么想法吗?

System.InvalidOperationException:传入字典的模型项的类型为“System.Web.Mvc.HandleErrorInfo”, 但是这个字典需要一个“BusinessLogic.Models.ErrorCodeModel”类型的模型项。 在 System.Web.Mvc.ViewDataDictionary`1.SetModel(对象值) 在 System.Web.Mvc.ViewDataDictionary..ctor(ViewDataDictionary 字典) 在 System.Web.Mvc.WebViewPage`1.SetViewData(ViewDataDictionary viewData) 在 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter writer,Object 实例) 在 System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext 上下文) 在 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext 控制器上下文,字符串 actionName) 在 System.Web.Mvc.Controller.ExecuteCore() 在 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) 在 System.Web.Mvc.MvcHandler.c__DisplayClass6.c__DisplayClassb.b__5() 在 System.Web.Mvc.Async.AsyncResultWrapper.c__DisplayClass1.b__0() 在 System.Web.Mvc.MvcHandler.c__DisplayClasse.b__d() 在 System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 在 System.Web.HttpApplication.ExecuteStep(IExecutionStep 步骤,Boolean& completedSynchronously)

在 global.asax.cs 中我注册了一个自定义属性:

公共静态无效RegisterGlobalFilters(GlobalFilterCollection过滤器) { filters.Add(new Controllers.ExtendendHandleErrorAttribute()); }

定义如下:

公共类 ExtendendHandleErrorAttribute : HandleErrorAttribute { 公共覆盖无效 OnException(ExceptionContext filterContext) { 日志错误(过滤器上下文); 尝试 { 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; } } 捕捉(异常前) { new LogManager().Log("ExtendendHandleErrorAttribute 错误", ex); } }

有趣的是,我创建了 ErrorCodeModel。

私有 ErrorCodeModel CreateModel(ExceptionContext filterContext) { var model = new ErrorCodeModel(); if (filterContext.HttpContext.Session != null) { var session = filterContext.HttpContext.Session; 模型.SessionId = 会话.SessionID; StateHandler stateHandler = new StateHandler(session); model.FapiErrorCode = stateHandler.CustomErrorCode.ToString(); 尝试 { model.GlobalData = new GlobalDataBuilder(stateHandler).Build(); model.ErrorMessage = model.GlobalData.ErrorText.TechnicalError; } 抓住 { } } 返回模型; }

我的 Web.config

【问题讨论】:

  • 你能简要解释一下你的场景,你想要实现什么,你做了什么来实现它,什么没有奏效?好吧,对于最后一部分,您已经显示了错误消息。

标签: asp.net-mvc-3


【解决方案1】:

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

@model System.Web.Mvc.HandleErrorInfo

@model BusinessLogic.Models.ErrorCodeModel

【讨论】:

  • 我的 Error.cshtml 中有一个自定义 @model,但我仍然收到错误消息。还有其他想法吗?
【解决方案2】:

当您在控制器或操作上使用[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>

【讨论】:

    【解决方案3】:
    猜你喜欢
    • 2013-10-18
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-11
    • 2018-08-15
    • 2021-08-06
    • 2014-05-20
    相关资源
    最近更新 更多