我使用Unity将一个新类实例化到控制器构造函数中,并将注入的类保存在控制器内的属性中.然后我想使用一个ActionFilter来查看注入的类是否具有我在其构造函数内验证的一些属性.那么有没有办法使用ActionFilter来验证注入类的属性?
提前致谢
解决方法
这样的事情?
public class ValidateActionFilterAttribute : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { var yourController = filterContext.Controller as YourController; if (yourController == null) { throw new InvalidOperationException("It is not YourController !!!"); } // It is YourController - validate here var yourProperty = yourController.YourProperty; // UPDATED *************************** // or test whether controller has property var property = filterContext.Controller.GetType().GetProperty("YourProperty"); if(property == null) { throw new InvalidOperationException("There is no YourProperty !!!"); } } }