Asp.net 从ModelValidator获取操作信息
我正在实现一个ModelValidator,它需要从执行操作中获得反映的信息。验证行为将根据操作的修饰方式而改变。我可以获取这些信息吗?您的ModelValidator的构造函数应该采用ControllerContext。您可以使用该对象确定控制器的装饰属性,如下所示:Asp.net 从ModelValidator获取操作信息,asp.net,asp.net-mvc-3,model-validation,Asp.net,Asp.net Mvc 3,Model Validation,我正在实现一个ModelValidator,它需要从执行操作中获得反映的信息。验证行为将根据操作的修饰方式而改变。我可以获取这些信息吗?您的ModelValidator的构造函数应该采用ControllerContext。您可以使用该对象确定控制器的装饰属性,如下所示: context.Controller.GetType().GetCustomAttributes(typeof(YourAttribute), true).Length > 0 编辑: 您还可以获得所有属性的列表,如下所
context.Controller.GetType().GetCustomAttributes(typeof(YourAttribute), true).Length > 0
编辑:
您还可以获得所有属性的列表,如下所示:
attributes = context.Controller.GetType().GetCustomAttributes(true);
下面是一个基于特定属性进行验证的简单示例:
public class SampleValidator : ModelValidator {
private ControllerContext _context { get; set; }
public SampleValidator(ModelMetadata metadata, ControllerContext context,
string compareProperty, string errorMessage) : base(metadata, context) {
_controllerContext = context;
}
public override IEnumerable<ModelValidationResult> Validate(object container) {
if (_context.Controller.GetType().GetCustomAttributes(typeof(YourAttribute), true).Length > 0) {
// do some custom validation
}
if (_context.Controller.GetType().GetCustomAttributes(typeof(AnotherAttribute), true).Length > 0) {
// do something else
}
}
}
公共类SampleValidator:ModelValidator{
私有控制器上下文_上下文{get;set;}
公共SampleValidator(ModelMetadata元数据、ControllerContext上下文、,
字符串比较属性,字符串错误消息):基(元数据,上下文){
_controllerContext=上下文;
}
公共重写IEnumerable验证(对象容器){
if(_context.Controller.GetType().GetCustomAttributes(typeof(YourAttribute),true).Length>0){
//做一些自定义验证
}
if(_context.Controller.GetType().GetCustomAttributes(typeof(AnotherAttribute),true)。长度>0){
//做点别的
}
}
}
}反编译System.Web.Mvc后,我得到了它:
protected override IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata, ControllerContext context, IEnumerable<Attribute> attributes)
{
ReflectedControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(context.Controller.GetType());
ReflectedActionDescriptor actionDescriptor = (ReflectedActionDescriptor) controllerDescriptor.FindAction(context, context.RouteData.GetRequiredString("action"));
object[] actionAttributes = actionDescriptor.GetCustomAttributes(typeof(MyAttribute), true);
}
受保护的重写IEnumerable GetValidator(ModelMetadata元数据、ControllerContext上下文、IEnumerable属性)
{
ReflectedControllerDescriptor controllerDescriptor=新的ReflectedControllerDescriptor(context.Controller.GetType());
ReflectedActionDescriptor actionDescriptor=(ReflectedActionDescriptor)controllerDescriptor.FindAction(上下文,context.RouteData.GetRequiredString(“操作”);
object[]actionAttributes=actionDescriptor.GetCustomAttributes(typeof(MyAttribute),true);
}
我们就快到了。我需要获取执行操作所用的属性。这将返回控制器的所有属性。哪种方法?我不知道你在问什么。我更新了我的答案,以展示一个简单的示例,说明这可能是如何工作的。在这个示例中,我需要实现getExecutingActionMethodInfo(),而不是_context.Controller:MethodInfo=getExecutingActionMethodInfo();如果(info.GetCustomAttributes(typeof(yourtattribute),true).Length>0)…爱德华多:关于主持人的回答,你不必把整个博客条目放在你的答案中。只需总结你博客的重要细节,并提供一个链接。简单链接的答案(没有摘要)会遇到一些问题;它们看起来像垃圾邮件,而且链接腐烂。