我有一个自定义的模型绑定器,我想获得的动作。因为我想获得使用反射动作的属性,动作的名称是远远不够的。
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模型绑定器,让操作方法
发布评论