asp.net-mvc-3 – System.Web.Mvc.HandleErrorInfo错误的模型类型

我创建了正确的模型类型,并不明白它的来源.有任何想法吗?
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

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....