基於此鏈接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