基于此链接https://msdn.microsoft.com/en-us/library/aa664615(v=vs.71).aspx我可以传递基本类型的单维数组。 但是,每当我传递一个字符串数组时,我都没有得到针对此自定义操作筛选器属性设置的所需值。有什么我做错了吗?
[AttributeUsage(AttributeTargets.Method, Inherited = true)]
public class CheckModelForNullAttribute : ActionFilterAttribute
{
private readonly Func<Dictionary<string, object>, string[], bool> _validate;
public string ErrorMessage { get; set; }
public string ErrorCode { get; set; }
public string[] ExcludeArgs { get; set; }
public CheckModelForNullAttribute( )
: this((objects, excludedArgs) => objects.ContainsValue(null) && excludedArgs.Any(objects.ContainsKey) == false)
{
}
public CheckModelForNullAttribute(Func<Dictionary<string, object>, string[], bool> checkCondition)
{
_validate = checkCondition;
}
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (_validate(actionContext.ActionArguments, ExcludeArgs))
{
actionContext.ModelState.AddModelError("EmptyModel", ErrorMessage);
actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, actionContext.ModelState.ValidationErrors(ErrorCode));
}
}
}
它用作:
[CheckModelForNullAttribute(ExcludeArgs = new[] { "requests"}, ErrorMessage = Constants.GenericErrors.DefaultError )]
public HttpResponseMessage Create([FromBody] CreateAccountsRequest requests)
{ }
当光标到达_validate(...)条件时调试字符串数组的值为空白,因为我在ErrorMessage
变量中有一个必需的值。