查询字符串模型绑定 ASP.NET WebApi
Querystring Model Binding ASP.NET WebApi
我有以下型号
public class Dog
{
public string NickName { get; set; }
public int Color { get; set; }
}
我有以下 api 控制器方法,它通过 API
公开
public class DogController : ApiController
{
// GET /v1/dogs
public IEnumerable<string> Get([FromUri] Dog dog)
{ ...}
现在,我想发出如下 GET 请求:
GET http://localhost:90000/v1/dogs?nick_name=Fido&color=1
问:如何将查询字符串参数nick_name绑定到狗class中的属性昵称?我知道我可以调用不带下划线(即昵称)的 API 或将 NickName 更改为 Nick_Name 并获取值,但我需要名称保持这样的约定。
编辑
这个问题不是重复的,因为它是关于 ASP.NET WebApi 而不是 ASP.NET MVC 2
实施 IModelBinder
、
public class DogModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(Dog))
{
return false;
}
var model = (Dog)bindingContext.Model ?? new Dog();
var hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
var searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";
model.NickName = GetValue(bindingContext, searchPrefix, "nick_name");
int colorId = 0;
if (int.TryParse(GetValue(bindingContext, searchPrefix, "colour"), out colorId))
{
model.Color = colorId; // <1>
}
bindingContext.Model = model;
return true;
}
private string GetValue(ModelBindingContext context, string prefix, string key)
{
var result = context.ValueProvider.GetValue(prefix + key); // <4>
return result == null ? null : result.AttemptedValue;
}
}
并创建 ModelBinderProvider
,
public class DogModelBinderProvider : ModelBinderProvider
{
private CollectionModelBinderProvider originalProvider = null;
public DogModelBinderProvider(CollectionModelBinderProvider originalProvider)
{
this.originalProvider = originalProvider;
}
public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
{
// get the default implementation of provider for handling collections
IModelBinder originalBinder = originalProvider.GetBinder(configuration, modelType);
if (originalBinder != null)
{
return new DogModelBinder();
}
return null;
}
}
并在控制器中使用类似的东西,
public IEnumerable<string> Get([ModelBinder(typeof(DogModelBinder))] Dog dog)
{
//controller logic
}
我有以下型号
public class Dog
{
public string NickName { get; set; }
public int Color { get; set; }
}
我有以下 api 控制器方法,它通过 API
公开public class DogController : ApiController
{
// GET /v1/dogs
public IEnumerable<string> Get([FromUri] Dog dog)
{ ...}
现在,我想发出如下 GET 请求:
GET http://localhost:90000/v1/dogs?nick_name=Fido&color=1
问:如何将查询字符串参数nick_name绑定到狗class中的属性昵称?我知道我可以调用不带下划线(即昵称)的 API 或将 NickName 更改为 Nick_Name 并获取值,但我需要名称保持这样的约定。
编辑 这个问题不是重复的,因为它是关于 ASP.NET WebApi 而不是 ASP.NET MVC 2
实施 IModelBinder
、
public class DogModelBinder : IModelBinder
{
public bool BindModel(HttpActionContext actionContext, ModelBindingContext bindingContext)
{
if (bindingContext.ModelType != typeof(Dog))
{
return false;
}
var model = (Dog)bindingContext.Model ?? new Dog();
var hasPrefix = bindingContext.ValueProvider.ContainsPrefix(bindingContext.ModelName);
var searchPrefix = (hasPrefix) ? bindingContext.ModelName + "." : "";
model.NickName = GetValue(bindingContext, searchPrefix, "nick_name");
int colorId = 0;
if (int.TryParse(GetValue(bindingContext, searchPrefix, "colour"), out colorId))
{
model.Color = colorId; // <1>
}
bindingContext.Model = model;
return true;
}
private string GetValue(ModelBindingContext context, string prefix, string key)
{
var result = context.ValueProvider.GetValue(prefix + key); // <4>
return result == null ? null : result.AttemptedValue;
}
}
并创建 ModelBinderProvider
,
public class DogModelBinderProvider : ModelBinderProvider
{
private CollectionModelBinderProvider originalProvider = null;
public DogModelBinderProvider(CollectionModelBinderProvider originalProvider)
{
this.originalProvider = originalProvider;
}
public override IModelBinder GetBinder(HttpConfiguration configuration, Type modelType)
{
// get the default implementation of provider for handling collections
IModelBinder originalBinder = originalProvider.GetBinder(configuration, modelType);
if (originalBinder != null)
{
return new DogModelBinder();
}
return null;
}
}
并在控制器中使用类似的东西,
public IEnumerable<string> Get([ModelBinder(typeof(DogModelBinder))] Dog dog)
{
//controller logic
}