asp.net-mvc-3 – System.Web.Mvc.HandleErrorInfo错误的模型类型
发布时间:2020-08-02 00:29:36 所属栏目:asp.Net 来源:互联网
导读:我创建了正确的模型类型,并不明白它的来源.有任何想法吗? System.InvalidOperationException: The model item passed into the dictionary is of type System.Web.Mvc.HandleErrorInfo, but this dictionary requires a model item of typ
我创建了正确的模型类型,并不明白它的来源.有任何想法吗? 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()); } 定义如下: 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. 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> 解决方法在/ Views / Shared / Error.cshtml中替换第一行:@model System.Web.Mvc.HandleErrorInfo 同 @model BusinessLogic.Models.ErrorCodeModel (编辑:岳阳站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 转:[WebServices]介绍
- asp.net-mvc-3 – RedirectResult Object作为URL中的参数
- asp-classic – 经典的asp / asp.net网站 – global.asa无效
- asp.net-core – ASP.Net 5类库中的EntityFramework命令?
- asp.net-mvc – 缩小ASP.NET MVC中的Action Filter属性
- 编辑并在ASP.NET Web项目中继续
- asp.net-mvc – 使用AWS .NET SDK进行SNS订阅确认示例
- asp.net删除文件session丢失
- 模型 – 视图 – 控制器 – ASP.NET WebForms vs MVC [VS20
- asp.net-mvc – 从视图到控制器POST信用卡数据是否安全?
推荐文章
站长推荐
- asp.net-mvc-3 – DropDownListFor Unobtrusive
- asp.net – “2015年4月20日Google帐户的OpenID2
- asp.net-mvc – Nhibernate / MVC:在View中处理
- .net – ReportViewer 2010无法评估表达式
- asp.net – 在Azure网站上启用gzip压缩
- asp.net-mvc – 在MVC4中为ViewModel设置默认值的
- 在HiddenField asp.net中存储一个字符串数组
- asp.net – 绑定ListView InsertItemTemplate中的
- asp.net汉字转拼音和获取汉字首字母的代码
- asp.net-mvc – 如何在RegularExpression中忽略大
热点阅读