我有一个自定义 ModelBinder,我想获得该操作。因为我想使用反射来获取操作的属性,所以操作名称是不够的。
我的行动方法:
[MyAttribute]
public ActionResult Index([ModelBinder(typeof(MyModelBinder))] MyModel model)
{
}
这里是一个典型的 ModelBinder
public class MyModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// here i would like to get the action method and his "MyAttribute"
}
}
有什么建议,其他解决方案吗?
提前谢谢了
不,您无法 100% 确定从模型绑定器获取当前操作。模型绑定器不耦合到操作,而是绑定到模型。例如,您可以调用
TryUpdateMode(model)
在选择操作之前的过滤器中。另请注意,操作方法甚至可能不是 CLR 方法(请参阅http://haacked.com/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx http://haacked.com/archive/2009/02/17/aspnetmvc-ironruby-with-filters.aspx)可以反映出来。
我认为真正的问题是,你到底想实现什么目标,这是正确的方法吗?如果您希望将来自操作的信息传递到模型绑定器(请注意如果信息不存在,您的模型绑定器应正常降级的建议),您应该使用操作过滤器将信息放入 HttpContext.Items (或类似的地方)然后让你的活页夹取回它。
操作过滤器的 OnActionExecuting 方法接收具有 ActionDescriptor 的 ActionExecutingContext。您可以对此调用 GetCustomAttributes。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)