现在,我想使用.Net MVC6创建一个新的api,但是我不能为它们更改所有客户端以使用不同的Uri。
我需要使我的新api与这类requests.The查询风格兼容如下:localhost/api/locations?location[lat]=12&location[lng]=21
public class Location
{
public string lat;
public string lng;
}
[Route("api/[controller]")]
public class LocationsController : Controller
{
[HttpGet]
public ActionResult Get(Location loc)
{
var av= new CreatedAtActionResult("","",null,null);
return av;
}
}我的问题是,如何将这种“类”查询绑定到参数上?
发布于 2016-03-11 15:58:54
简单的做法是重写参数名称:
public ActionResult Get([FromUri(Name = "location[lat]")]string lat, [FromUri(Name = "location[lng]")]string lng)
{
}另外,您可以实现自定义的TypeConverter或ModelBinder。
您可以在这里找到关于所有不同可能性的很好的概述:http://www.asp.net/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api
更新:我在Changing the parameter name Web Api model binding上发现了一个类似的问题
发布于 2016-03-11 15:48:49
您可以通过这样做来调用api:
api/locations?lat=xxx&lng=zzz
https://stackoverflow.com/questions/35944413
复制相似问题