mar*_*ras 9 asp.net-core-1.0 asp.net5
你如何建模在ASP.NET Core 1 Web API中使用GET从URI中绑定数组(隐式或显式)?
在ASP.NET Web API pre Core 1中,这有效:
[HttpGet]
public void Method([FromUri] IEnumerable<int> ints) { ... }
Run Code Online (Sandbox Code Playgroud)
你是如何在ASP.NET Web API Core 1(又名ASP.NET 5又名ASP.NET vNext)中做到这一点的?文档什么都没有.
Wil*_*Ray 14
本FromUriAttribute类综合FromRouteAttribute和FromQueryAttribute类.根据路由的配置/发送的请求,您应该能够用其中一个替换您的属性.
但是,有一个垫片可供你FromUriAttribute上课.通过软件包资源管理器安装"Microsoft.AspNet.Mvc.WebApiCompatShim"NuGet软件包,或将其直接添加到project.json文件中:
"dependencies": {
"Microsoft.AspNet.Mvc.WebApiCompatShim": "6.0.0-rc1-final"
}
Run Code Online (Sandbox Code Playgroud)
虽然它有点旧,但我发现这篇文章在解释一些变化方面做得非常好.
如果您要为数组绑定逗号分隔值("/ api/values?ints = 1,2,3"),则需要像以前一样使用自定义绑定器.这是适用于ASP.NET Core 的Mrchief解决方案的改编版本.
public class CommaDelimitedArrayModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext.ModelMetadata.IsEnumerableType)
{
var key = bindingContext.ModelName;
var value = bindingContext.ValueProvider.GetValue(key).ToString();
if (!string.IsNullOrWhiteSpace(value))
{
var elementType = bindingContext.ModelType.GetTypeInfo().GenericTypeArguments[0];
var converter = TypeDescriptor.GetConverter(elementType);
var values = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => converter.ConvertFromString(x.Trim()))
.ToArray();
var typedValues = Array.CreateInstance(elementType, values.Length);
values.CopyTo(typedValues, 0);
bindingContext.Result = ModelBindingResult.Success(typedValues);
}
else
{
// change this line to null if you prefer nulls to empty arrays
bindingContext.Result = ModelBindingResult.Success(Array.CreateInstance(bindingContext.ModelType.GetElementType(), 0));
}
return TaskCache.CompletedTask;
}
return TaskCache.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
您可以指定要用于所有集合的模型绑定器Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc().AddMvcOptions(opts =>
{
opts.ModelBinders.Insert(0, new CommaDelimitedArrayModelBinder());
});
}
Run Code Online (Sandbox Code Playgroud)
或者在API调用中指定一次:
[HttpGet]
public void Method([ModelBinder(BinderType = typeof(CommaDelimitedArrayModelBinder))] IEnumerable<int> ints)
Run Code Online (Sandbox Code Playgroud)
@ WillRay的回答有点过时了.我写了一个'IModelBinder'和'IModelBinderProvider'.第一个可以与[ModelBinder(BinderType = typeof(DelimitedArrayModelBinder))]属性一起使用,而第二个可以用于全局应用模型绑定,如下所示.
.AddMvc(options =>
{
// Add to global model binders so you don't need to use the [ModelBinder] attribute.
var arrayModelBinderProvider = options.ModelBinderProviders.OfType<ArrayModelBinderProvider>().First();
options.ModelBinderProviders.Insert(
options.ModelBinderProviders.IndexOf(arrayModelBinderProvider),
new DelimitedArrayModelBinderProvider());
})
public class DelimitedArrayModelBinderProvider : IModelBinderProvider
{
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
if (context == null)
{
throw new ArgumentNullException(nameof(context));
}
if (context.Metadata.IsEnumerableType && !context.Metadata.ElementMetadata.IsComplexType)
{
return new DelimitedArrayModelBinder();
}
return null;
}
}
public class DelimitedArrayModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
{
throw new ArgumentNullException(nameof(bindingContext));
}
var modelName = bindingContext.ModelName;
var valueProviderResult = bindingContext.ValueProvider.GetValue(modelName);
var values = valueProviderResult
.ToString()
.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
var elementType = bindingContext.ModelType.GetTypeInfo().GenericTypeArguments[0];
if (values.Length == 0)
{
bindingContext.Result = ModelBindingResult.Success(Array.CreateInstance(elementType, 0));
}
else
{
var converter = TypeDescriptor.GetConverter(elementType);
var typedArray = Array.CreateInstance(elementType, values.Length);
try
{
for (int i = 0; i < values.Length; ++i)
{
var value = values[i];
var convertedValue = converter.ConvertFromString(value);
typedArray.SetValue(convertedValue, i);
}
}
catch (Exception exception)
{
bindingContext.ModelState.TryAddModelError(
modelName,
exception,
bindingContext.ModelMetadata);
}
bindingContext.Result = ModelBindingResult.Success(typedArray);
}
return Task.CompletedTask;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6251 次 |
| 最近记录: |