当前位置: 首页 > 编程语言 > C#

糟糕!为什么要将System.Web.Mvc.HandleErrorInfo传递到我的视图?分享

时间:2023-04-10 15:55:39 C#

哎呀!为什么要将System.Web.Mvc.HandleErrorInfo传递到我的视图?我遇到了一个相当令人沮丧的问题。我的MVC网站大部分工作正常,但随机抛出错误(向用户显示的友好错误)。当我检查日志时,这是我得到的:System.InvalidOperationException:传入字典的模型项的类型为“System.Web.Mvc.HandleErrorInfo”,但该字典需要“BaseViewData”类型的模型项。一段时间后,同一用户可以点击刷新,页面加载正常。我卡住了。(更新:添加堆栈跟踪System.Web.HttpUnhandledException:引发了“System.Web.HttpUnhandledException”类型的异常。--->System.InvalidOperationException:传入字典的模型项的类型为“System.Web.Mvc.HandleErrorInfo”,但该字典需要“BaseViewData”类型的模型项。在System.Web.Mvc.ViewDataDictionary`1.SetModel(对象值),RouteCollectionrouteCollection)在System.Web.Mvc.ViewMasterPage`1.get_Html()在ASP.views_shared_site_master.__Render__control1(HtmlTextWriter__w,ControlparameterContainer)在System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriterwriter,ICollectionchildren)在System.Web.UI.Control.RenderChildren(HtmlTextWriter作者)在System.Web.UI.Control.Render(HtmlTextWriter作者)在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter编写器,ControlAdapter适配器)位于System.Web.UI.Control.RenderControl(HtmlTextWriter编写器,ControlAdapter适配器)位于System.Web.UI.Control.RenderControl(HtmlTextWriter编写器)位于System.Web.UI.Control。System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter编写器,ICollection子级)在System.Web.UI.Page.Render(HtmlTextWriter编写器)在System.Web.Mvc.ViewPage.Render(HtmlTextWriter编写器)在System.Web.UI.Control.RenderControlInternal(HtmlTextWriter编写器,ControlAdapter适配器)在System.Web.UI.Control.RenderControl(HtmlTextWriter编写器,ControlAdapter适配器).Web.UI.Page.ProcessRequestMain(BooleanincludeStagesBeforeAsyncPoint,BooleanincludeStagesAfterAsyncPoint)---内部异常堆栈跟踪结束---在System.Web.UI.Page.HandleError(Exceptione)在System.Web.UI.Page。过程essRequestMain(布尔值includeStagesBeforeAsyncPoint,布尔值includeStagesAfterAsyncPoint)在System.Web.UI.Page.ProcessRequest(布尔值includeStagesBeforeAsyncPoint,布尔值includeStagesAfterAsyncPoint)在System.Web.UI.Page.ProcessRequest()在System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext上下文))在System.Web.UI.Page.ProcessRequest(HttpContext上下文)在ASP.views_shared_error_aspx.ProcessRequest(HttpContext上下文)在System.Web.Mvc.ViewPage.RenderView(ViewContextviewContext)ViewContext上下文,ViewPage页)在System.Web.Mvc.WebFormView.Render(ViewContextviewContext,TextWriterwriter)在System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext上下文),ActionResultactionResult)在System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContextcontrollerContext,StringactionName)在System.Web.Mvc.Controller.ExecuteCore()在System.Web.Mvc.ControllerBase.Execute(RequestContextrequestContext))在System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBasehttpContext)在System.Web.Mvc.MvcHandler.ProcessRequest(HttpContexthttpContext)System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()atSystem.Web.HttpApplication.ExecuteStep(IExecutionStepstep,Boolean&completedSynchronously)Hereisanissueoncodeplexexplainingwhytheerroroccurs引自http://web.archive.org/web/20131004122626/http://aspnet.codeplex.com/workitem/1795因为原链接失效了:HandleErrorattributeshouldnotstoreexceptioninformationinViewDatawhenHandleErrorattributehandlesexception,itwillstoreexceptionViewData中的信息。当Error.aspxin继承自site.master并且site.master类声明如下时,这是一个问题。publicpartialclassSite:System.Web.Mvc.ViewMasterPage{}SiteViewData包含:publicclassSiteViewData{publicStringTitle{get;放;}}每个页面ViewData类继承自SiteViewData类,看起来像这样publicclassIndexViewData:SiteViewData{publicStringMessage{get;放;}publicStringSupportedLanguages{get;set;}}此方法允许用户在Site.Master页面中编写代码,如下所示不幸的是,当抛出异常时,模型已经被HandleErrorInfo类的实例替换。这会导致InvalidOperationException被抛出,其中包含传递到字典中的模型项的类型为System.Web.Mvc.HandleErrorInfo的信息,但此字典需要Igwt.Boh.Website.Web.Controllers.SiteViewData类型的模型项。是否可以向ViewResult类添加新的ErrorData属性来存储HandleErrorInfo类的实例?这样ViewData就不会改变。初始化IndexViewData(和SiteViewData)属性后,操作中抛出的任何异常都极有可能发生。无法修复它于2010年1月27日中午12:24关闭-查看评论。“wontfix”中提到的评论来自微软团队的一名前成员,他们的修复建议(粗体):在[HandleError]属性执行时,我们已经丢失了对原始ActionResult对象的引用。我们甚至不知道您是否打算显示视图-也许您打算重定向。管道中的部分(ViewResult)负责将模型从控制器传递到视图。如果发生异常,应用程序正在处理的任何模型都应被视为已损坏或无法使用。最佳做法是编写错误视图,使其及其依赖项(如母版页)都不需要原始模型。我解决这个问题的方法是删除布局页面顶部的@model指令,然后进行一些检查,我通常希望看到我的模型在可能传入的不同模型之间切换,例如@if(ModelisSystem.Web.Mvc.HandleErrorInfo){Error}elseif(Model.GetType()==typeof(MyApp.Models.BaseViewModel)){@Model.PageTitleComplete}我刚刚在我的应用程序中发现了类似的问题并想帮助我描述修复。在我的例子中,我得到以下异常:System.InvalidOperationException:传递到字典中的模型项的类型为“System.Web.Mvc.HandleErrorInfo”,但该字典需要一个类型为“Web.Models.Admin.Login”的模型项'.我正在使用[HandleError]将错误路由到~/Shared/Error.cshtml发生的事情[至少在我的情况下]是:~/Shared/Error.cshtmlhasLayout="~/Views/SiteLayout.cshtml";确保错误页面的样式正确(与站点的其余部分一样),而无需重复布局/CSS包含。~/Views/SiteLayout.cshtml包含以下部分:~/Shared/LightboxLogin.cshtml,它提供用于登录的内联灯箱。~/Shared/LightboxLogin.cshtml进一步部分嵌入了实际的登录表单:@Html.Partial("Login"),其中包含~/Shared/Login.cshtml,用于前端站点的登录功能。因为错误是在站点的Admin区引发的,controller是“Admin”,当错误发生时,会调用HandleErrorInfo,其中包含一个HandleErrorInfo和一个HandleErrorInfo模型。这又包括LightboxLogin,然后包括Partial、Login...但是~/Admin/Login.cshtml有另一个视图,它由@Html.Partial("Login")包含。~/Admin/Login.cshtml这个视图有:@modelWeb.Models.Admin.Login所以这里吸取的教训是要注意你想要包含的部分的命名。如果~/Shared/Login.cshtml为~/Shared/PublicLoginForm.cshtml并且使用@Html.Partial("PublicLoginForm")就可以避免这个问题。旁注:我修复了这个[因为我不想重构我的视图]:@if(!(ModelisHandleErrorInfo)){@Html.Partial("LightboxLogin")}这意味着当布局包含在错误中时条件,不包括部分。我在强类型视图中遇到了这个错误,并通过设置原始请求上下文的RouteData.Values["controller"]和"action"来匹配错误页面控制器和操作名称来修复它。如果您看这里,您会看到一个增强的HandleErrorAttribute实现,除了JSON支持之外,它还会向您显示基类中的内容以及结果视图。https://www.dotnettricks.com/learn/mvc/exception-or-error-handling-and-logging-in-mvc4如果这里的ViewResult构造和微软使用的逻辑类似,问题可能在于它可以仅指定一个新的(错误条件)视图,而不是控制器或操作(因为它已从原始请求更改)。也许这就是MVC框架/处理程序与类型化视图混淆的原因。这对我来说似乎是一个错误。上面的示例不包含此修复,因此您必须按如下方式对其进行编辑(最后两行和注释是新的):varmodel=newHandleErrorInfo(httpError,controllerName,actionName);filterContext.Result=newViewResult{ViewName=View,MasterName=Master,ViewData=newViewDataDictionary(model),TempData=filterContext.Controller.TempData};//在需要时更正路由数据,例如防止类型化视图出错filterContext.RouteData.Values["controller"]="MyError";//MyErrorController.Index(HandleErrorInfo)filterContext.RouteData.Values["action"]="Index";如果你没有在过滤器/属性中处理它,那么你只需要像最后两行那样处理路由数据,例如,许多“OnError”示例构造一个错误控制器,然后调用IContoller。执行。但那是另一回事了。无论如何,如果您收到此错误,无论您在哪里处理错误,只需将原始的“Controller”和“Action”名称重置为您正在使用的任何名称,它也应该会为您修复它。以上就是C#学习教程:哎呀!为什么要将System.Web.Mvc.HandleErrorInfo传递到我的视图?如果所有分享的内容对你有用,需要进一步了解C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: