ActionFilter 内的模型可用性
我已经为我正在创建的 ASP.NET MVC 应用程序创建了一个新的 ActionFilter。我有一个接受 Http Post 的操作,并且该操作方法的参数接受一个对象,我已为其创建并注册了一个自定义模型绑定程序。
我注意到,在 IActionFilter.OnActionExecuting
中,filterContext.Controller.ViewData.Model
的值始终为 null
,尽管事实上它看起来像模型绑定器始终在操作过滤器 OnActionExecuting
方法之前调用。与此相反,在同一操作过滤器的 IActionFilter.OnActionExecuted
方法中,filterContext.Controller.ViewData.Model
的值不为 null。
你们知道这是设计使然还是错误吗?如果按照设计,他们有任何链接可以描述这是为什么吗?谢谢。
I have created a new ActionFilter for an ASP.NET MVC application that I'm creating. I have an action which accepts an Http Post and the argument of the action method accepts an object, for which I have created and registered a custom model binder.
I noticed that inside the IActionFilter.OnActionExecuting
the value for filterContext.Controller.ViewData.Model
is always null
despite the fact that it looks like the model binder is always invoked before the action filter OnActionExecuting
method. In contrast to this inside the IActionFilter.OnActionExecuted
method of the same action filter the value for filterContext.Controller.ViewData.Model
is not null.
Do you guys know if this is by design or a bug? If by design are their any links which describe why this is? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也就是说,您通常在执行操作期间设置模型。因此,在调用操作之前发生的 OnActionExecuting 执行期间,模型自然为 null。
Sayed, you usually set a model during the execution of an action. Therefore, it is natural that the model is null during the execution of the OnActionExecuting which occurs before the action is called.
既然控制器操作负责创建模型并将其传递给视图,那么模型如何在调用操作之前就存在呢?在模型发生之前,您不知道将要创建什么模型。
Since the Controller Action is responsible for creating the Model and passing it to the View how could the Model exist prior to the action being called? You don't know what Model is going to be created until after it's happened.