gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:38:31 24 4
gpt4 key购买 nike

基于此链接 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 变量中有一个必需的值。 enter image description here

最佳答案

我的测试-

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

enter image description here

关于c# - 无法将字符串数组传递给 WebApi Actionfilter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36638156/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com