我想为我的 Web API 创建一个全局验证属性。所以我按照教程完成了以下属性:
public class ValidationActionFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid)
{
return;
}
var errors = new List<KeyValuePair<string, string>>();
foreach (var key in actionContext.ModelState.Keys.Where(key =>
actionContext.ModelState[key].Errors.Any()))
{
errors.AddRange(actionContext.ModelState[key].Errors
.Select(er => new KeyValuePair<string, string>(key, er.ErrorMessage)));
}
actionContext.Response =
actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);
}
}
然后我将它添加到 Global.asax 中的全局过滤器中:
configuration.Filters.Add(new ValidationActionFilter());
它适用于我的大多数操作,但不适用于具有可选和可为空请求参数的操作。
例如:
我创建了一条路线:
routes.MapHttpRoute(
name: "Optional parameters route",
routeTemplate: "api/{controller}",
defaults: new { skip = UrlParameter.Optional, take = UrlParameter.Optional });
我的一个动作ProductsController
:
public HttpResponseMessage GetAllProducts(int? skip, int? take)
{
var products = this._productService.GetProducts(skip, take, MaxTake);
return this.Request.CreateResponse(HttpStatusCode.OK, this._backwardMapper.Map(products));
}
现在,当我请求此 url 时:http://locahost/api/products
我收到带有 403 状态代码和以下内容的响应:
[
{
"Key": "skip.Nullable`1",
"Value": "A value is required but was not present in the request."
},
{
"Key": "take.Nullable`1",
"Value": "A value is required but was not present in the request."
}
]
我相信这不应该显示为验证错误,因为这些参数都是可选的和可为空的。
有没有人遇到过这个问题并找到了解决方案?