从ActionFilterAttribute

时间:2018-06-13 20:45:30

标签: c# asp.net .net asp.net-web-api actionfilterattribute

给定自定义 ActionFilterAttribute ,是否有办法在重写OnActionExecuting方法中使用 HttpActionContext 访问操作模型上的属性?

如果属性不是特定类型,我希望抛出异常。

例如:

存在名为[CustomRequired]的自定义属性。如果模型上有[Required]的任何用法,则抛出异常。该模型应该使用[CustomRequired],而不是[Required]

public class ModelValidationAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        ValidateModelAttributes(actionContext);
    }

    private static void ValidateModelAttributes(HttpActionContext actionContext)
    {
        var attrs = actionContext.ActionDescriptor.GetParameters();
        foreach (var attr in attrs) 
        {
             // if attr is not type then throw exception
        }
    }
}

0 个答案:

没有答案
相关问题