gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:05:28 26 4
gpt4 key购买 nike

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

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

关于asp.net-mvc-3 - System.Web.Mvc.HandleErrorInfo 模型类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5742141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com