无法将字符串数组传递给WebApi Actionfilter

问题描述 投票:0回答:2

基于此链接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
变量中有所需的值。

c# asp.net-web-api asp.net-web-api2 action-filter
2个回答
1
投票

我的测试-

[CheckModelForNullAttribute(ExcludeArgs = new string[] { "Test" }, ErrorMessage = "error", ErrorCode = "404")]


0
投票

同样的问题...您找到解决方案了吗...? Tnx

© www.soinside.com 2019 - 2024. All rights reserved.