ActionFilter 内的模型可用性

发布于 2024-08-24 22:52:47 字数 496 浏览 10 评论 0原文

我已经为我正在创建的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我是有多爱你 2024-08-31 22:52:47

也就是说,您通常在执行操作期间设置模型。因此,在调用操作之前发生的 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.

不美如何 2024-08-31 22:52:47

既然控制器操作负责创建模型并将其传递给视图,那么模型如何在调用操作之前就存在呢?在模型发生之前,您不知道将要创建什么模型。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文