System.Web.Mvc.HandleErrorInfo错型号类型 [英] System.Web.Mvc.HandleErrorInfo wrong model type

查看:925
本文介绍了System.Web.Mvc.HandleErrorInfo错型号类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建正确的型号和不明白的地方这从何而来。任何想法?


System.InvalidOperationException:传递到字典的模型项的类型是System.Web.Mvc.HandleErrorInfo,
但本词典需要类型的模型项目BusinessLogic.Models.Error codeModel。
   在System.Web.Mvc.ViewDataDictionary`1.SetModel(对象的值)
   在System.Web.Mvc.ViewDataDictionary..ctor(的ViewDataDictionary字典)
   在System.Web.Mvc.WebViewPage`1.SetViewData(可视数据的ViewDataDictionary)
   在System.Web.Mvc.RazorView.RenderView(ViewContext viewContext,TextWriter的作家,Object实例)
   在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文)
   在System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext 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一步,布尔和completedSynchronously)

在我的global.asax.cs注册自定义属性:


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

的定义是这样的:


公共类ExtendendHandleErrorAttribute:HandleErrorAttribute
    {
        公共覆盖无效onException的(ExceptionContext filterContext)
        {
            LogErrors(filterContext);
            尝试
            {
                base.OnException(filterContext);                VAR typedResult = filterContext.Result为的ViewResult;
                如果(typedResult!= NULL)
                {
                    VAR tmpModel = typedResult.ViewData.Model;
                    typedResult.ViewData = filterContext.Controller.ViewData;
                    typedResult.ViewData.Model = CreateModel(filterContext);
                    filterContext.Result = typedResult;
                }
            }
            赶上(异常前)
            {
                新的日志管理()日志(ExtendendHandleErrorAttribute错误,前);
            }
        }

在这里有趣的是,我创建Error codeModel。


        私人错误codeModel CreateModel(ExceptionContext filterContext)
        {
            VAR模型=新的错误codeModel();            如果(filterContext.HttpContext.Session!= NULL)
            {
                VAR会话= filterContext.HttpContext.Session;                model.SessionId = session.SessionID;
                StateHandler stateHandler =新StateHandler(会话);
                model.FapiError code = stateHandler.CustomError code.ToString();
                尝试
                {
                    model.GlobalData =新GlobalDataBuilder(stateHandler).Build();
                    model.ErrorMessage = model.GlobalData.ErrorText.TechnicalError;
                }
                赶上{}
            }            回归模型;
        }

我的web.config


<的customErrors模式="关闭"的defaultRedirect ="错误">
      <错误状态code = QUOT; 404 QUOT;重定向="错误/ FileNotFound" />
    < /&的customErrors GT;


解决方案

〜/查看/共享/ Error.cshtml 替换第一行:

  @model System.Web.Mvc.HandleErrorInfo

  @model BusinessLogic.Models.Error codeModel

i create the correct model type and dont understand where this comes from. any ideas?

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)

in the global.asax.cs i register a custom attribute:

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

the definition looks like this:

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

interesting here that i create the 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;
        }

my Web.config

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

解决方案

In ~/Views/Shared/Error.cshtml replace the first line:

@model System.Web.Mvc.HandleErrorInfo

with

@model BusinessLogic.Models.ErrorCodeModel

这篇关于System.Web.Mvc.HandleErrorInfo错型号类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆