基于此链接https://msdn.microsoft.com/en-us/library/aa664615(v=vs.71).aspx我可以传递基元类型的单维数组。 但是,无论何时传递一个字符串数组,我都没有获得对此自定义操作筛选器属性设置的期望值。有什么我做错了吗?无法将字符串数组传递给WebApi Actionfilter
[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
所需的值变量。
对我工作的罚款。 –
@AmitKumarGhosh没有配偶! – codebased