Asp.NET MVC模型绑定器,让操作方法

编程入门 行业动态 更新时间:2024-10-24 09:17:44
本文介绍了Asp.NET MVC模型绑定器,让操作方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个自定义的模型绑定器,我想获得的动作。因为我想获得使用反射动作的属性,动作的名称是远远不够的。

I got a custom ModelBinder and i would like to get the action. Because i want to get the Attributes of the action using reflection, the action name is not enough.

我的操作方法:

[MyAttribute] public ActionResult Index([ModelBinder(typeof(MyModelBinder))] MyModel model) { }

和这里一个典型的ModelBinder的

and here a typically ModelBinder

public class MyModelBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { // here i would like to get the action method and his "MyAttribute" } }

任何建议,其他的解决方案?

any suggestions, other solutions ?

提前很多感谢

推荐答案

没有,你不能100%的把握获得一个模型绑定当前操作。该模型粘合剂不耦合到的动作,但是要结合到模型。例如,你可以叫

No, you cannot with 100% certainty get the current action from a model binder. The model binder is not coupled to the action, but to binding to a model. For example, you can call

TryUpdateMode(model)

在一个过滤器的动作已被选定之前。另外请注意,操作方法甚至可能不是一个CLR方法(见的 haacked/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx ),可以反映。

In an filter before an action has been chosen. Also note that an action method might not even be a CLR method (see haacked/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx) that can be reflected on.

我觉得真正的问题是,究竟是你想什么来完成,这是正确的做法?如果你想从动作的信息要传递给模型绑定(听取意见,你的模型绑定应该适度降低,如果信息不存在),你应该使用一个动作过滤器放在HttpContext.Items中的信息(或地方像),然后让粘结剂检索。

I think the real question is, what exactly are you trying to accomplish and is this the right way? If you want information from the action to be passed to the model binder (heeding the advice that your model binder should degrade gracefully if the information isn't there), you should use an action filter to put the information in HttpContext.Items (or somewhere like that) and then have your binder retrieve it.

这是动作过滤器的OnActionExecuting方法接收它有一个ActionDescriptor的ActionExecutingContext。你可以调用该GetCustomAttributes。

An action filter's OnActionExecuting method receives an ActionExecutingContext which has an ActionDescriptor. You can call GetCustomAttributes on that.

更多推荐

Asp.NET MVC模型绑定器,让操作方法

本文发布于:2023-11-05 07:46:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1560274.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   操作方法   模型   Asp   NET

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!