阻止ASP.NET MVC调用模型类中的所有getter
如果在模型类上定义以下两个属性,则在模型绑定期间将使用NullReferenceException崩溃:
public Customer Customer { get; private set; } //set in the action method public bool Name => Customer.Name; 这是因为在模型绑定期间Customer仍为null,而ASP.NET MVC为Name调用getter. 堆栈是: System.ComponentModel.ReflectPropertyDescriptor.GetValue(Object component) +525 System.Web.Mvc.ModelMetadata.get_Model() +34 System.Web.Mvc.DataAnnotationsModelValidator.Validate(Object container) +151 System.Web.Mvc.<Validate>d__1.MoveNext() +387 System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext,ModelBindingContext bindingContext) +163 System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext,ModelBindingContext bindingContext,Object model) +83 System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext,ModelBindingContext bindingContext) +1754 System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext,ParameterDescriptor parameterDescriptor) +460 System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext,ActionDescriptor actionDescriptor) +137 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext,String actionName) +982 System.Web.Mvc.<>c__DisplayClass22.<BeginExecuteCore>b__1e() +39 System.Web.Mvc.Async.AsyncResultWrapper.<.cctor>b__0(IAsyncResult asyncResult,Action action) +21 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +36 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +38 System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult,ProcessRequestState innerState) +44 System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +65 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +38 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +399 System.Web.HttpApplication.ExecuteStep(IExecutionStep step,Boolean& completedSynchronously) +137 从堆栈看,模型验证看起来像是在查询所有的getter.我没有使用模型验证. 我该如何处理这种情况?我可以让ASP.NET MVC在没有任何(明显)原因的情况下不调用所有getter吗? 解决方法
MVC版本5.2.3中的DefaultModelBinder除了绑定之外还进行验证,并且无法完全关闭它.其他SO帖子提到关闭值类型的隐式必需属性,使用Global.asax.cs,Application_Start()方法中的以下代码行…
DataAnnotationsModelValidatorProvider.AddImplicitRequiredAttributeForValueTypes = false; 请参阅:https://stackoverflow.com/a/2224651(直接从asp.net团队引用一个直接回答的论坛). 鉴于您的堆栈跟踪,这可能会解决您的直接问题.但是,这可能是不够的,因为DefaultModelBinder甚至在其验证代码之外调用getter没有任何明确的原因(源代码没有评论它为什么这样做). 为了解决我的项目中的问题,我一直使用计算属性,类似于您的示例,我实现了一个基于原始DefaultModelBinder源代码的自定义模型绑定器,该源代码不会调用任何getter. 请在此处查看更详细的说明和完整解决方案:https://stackoverflow.com/a/54431404/10987278. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – AutoEventWireup True Vs False
- asp.net-mvc – 查看模板代码标签 – asp.net mvc – regd
- asp.net-mvc – 通过Gitignore递归地包含Nuget DLL
- ASP.NET实现二维码
- asp.net – Windows Azure VIP Swap,如何处理自定义web.con
- ASP.NET – 控制事件不在Repeater内部触发
- asp.net – 如何在使用System.Web.Optimization时将type =“
- asp.net – 为jQuery寻找一个好的数据网格插件
- asp.net-mvc – ASP.NET MVC – Html.Textbox()throws“对象
- asp.net core 腾讯验证码的接入