我有一个Web API方法:
public List<Task> GetTasks([FromUri] TaskFilter filter)
{
}
该方法具有带可空标识符列表的参数:
public class TaskFilter
{
public IList<int?> Assignees { get; set; }
}
我打电话的时候:
GET /tasks?assignees=null
服务器返回错误:
{
"message":"The request is invalid.",
"modelState": {
"assignees": [ "The value 'null' is not valid for Nullable`1." ]
}
}
仅当我传递空字符串时才有效:
GET /tasks?assignees=
但标准查询字符串转换器(来自 JQuery, Angular等)不能以这种方式使用空值。
如何让ASP.NET将'null'
解释为null
?
更新:查询字符串可以包含多个标识符,例如:
GET /tasks?assignees=1&assignees=2&assignees=null
Upd2: JQuery将数组中的空值转换为空字符串,ASP.NET将它们解释为null。所以问题是从Angular 1.6($HttpParamSerializerProvider
)
Upd3:我了解变通方法,但我不要求它们。我想要针对特定问题的解决方案:
null
值List<int?>
,因为API文档是自动生成的,我不希望将文本数组视为参数类型JQuery.param
以这种方式工作)null
数组项转换为空字符串答案 0 :(得分:5)
您可以为此特定类型创建自定义模型绑定,从DefaultModelBinder继承,用于示例:
using System.ComponentModel.DataAnnotations;
using System.Web.Mvc;
public class TaskFilterBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, System.Web.Mvc.ModelBindingContext bindingContext)
{
var request = controllerContext.HttpContext.Request;
var assignees = request.QueryString["assignees"];
if (assignees == "null") // check if assignees is null (string) then return NULL
return null;
return assignees;
}
}
最后,我们需要告知控制器我们希望它使用的绑定。我们可以使用属性
指定[ModelBinder的(typeof运算(TaskFilterBinder))]
如下:
public List<Task> GetTasks([FromUri(ModelBinder=typeof(TaskFilterBinder))] TaskFilter filter)
{
// Do your stuff.
}
如需更多参考,请查看Custom Model Binders上的此链接。 希望,这解决了你的问题。谢谢
答案 1 :(得分:5)
最后,我找到了使用自定义值提供程序的解决方案:
using System;
using System.Collections.Generic;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.ValueProviders;
using System.Web.Http.ValueProviders.Providers;
using System.Globalization;
using System.Net.Http;
using System.Web.Http.ModelBinding;
public sealed class NullableValueProviderAttribute : ModelBinderAttribute
{
private readonly string[] _nullableColumns;
public NullableValueProviderAttribute(params string[] nullableColumns)
{
_nullableColumns = nullableColumns;
}
public override IEnumerable<ValueProviderFactory> GetValueProviderFactories(HttpConfiguration configuration)
{
return new ValueProviderFactory[] { new NullableValueProviderFactory(_nullableColumns) };
}
}
public class NullableValueProviderFactory : ValueProviderFactory, IUriValueProviderFactory
{
private readonly string[] _nullableColumns;
public NullableValueProviderFactory(string[] nullableColumns)
{
_nullableColumns = nullableColumns;
}
public override IValueProvider GetValueProvider(HttpActionContext actionContext)
{
return new NullableQueryStringValueProvider(actionContext, CultureInfo.InvariantCulture, _nullableColumns);
}
}
public class NullableQueryStringValueProvider : NameValuePairsValueProvider
{
private static readonly string[] _nullValues = new string[] { "null", "undefined" };
private static IEnumerable<KeyValuePair<string, string>> GetQueryNameValuePairs(HttpRequestMessage request, string[] nullableColumns)
{
foreach (var pair in request.GetQueryNameValuePairs())
{
var isNull = Array.IndexOf(nullableColumns, pair.Key) >= 0 && Array.IndexOf(_nullValues, pair.Value) >= 0;
yield return isNull ? new KeyValuePair<string, string>(pair.Key, "") : pair;
};
}
public NullableQueryStringValueProvider(HttpActionContext actionContext, CultureInfo culture, string[] nullableColumns) :
base(GetQueryNameValuePairs(actionContext.ControllerContext.Request, nullableColumns), culture)
{ }
}
并在Web API操作中指定它:
public List<Task> GetTasks([NullableValueProvider("assignees")] TaskFilter filter)
{
}