前端请求restful风格接口怎么传参,如何将多个参数传递/接收到RESTful Web API GET方法?...

The usual examples of GET RESTful methods that take a parameter (returning a scalar value rather than a dataset) are shown like so:

public string Get(int id)

{

//get and return the value

}

...where the val passed is typically an ID, so you can use it to get a scalar value based on that unique value.

What, though, if you want to pass multiple values, such as a string and an int? Is it simply a matter of defining a method like so:

public string Get(string someString, int someInt)

{

//get and return the value

}

...and calling it like so:

//const string uri = "http://192.112.183.42:80/api/platypusItems/someString/someInt";, zB:

const string uri = "http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42";

var webRequest = (HttpWebRequest) WebRequest.Create(uri);

?

IOW, will the routing mechanism figure out that, since two args are passed, it should call the Get() method with two args ("convention over configuration"), or is there more that has to be done to route things appropriately?

解决方案

If you use Web API 2, then you can use Attribute Routing to route requests like http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42

public class ItemsController : ApiController

{

[Route("api/{controller}/{id}")]

public string GetItemById(int id)

{

// Find item here ...

return item.ToString();

}

[Route("api/{controller}/{name}/{id}")]

public string GetItemByNameAndId(string name, int id)

{

// Find item here ...

return item.ToString();

}

}

http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42 will be mapped to GetItemByNameAndId while http://192.112.183.42:80/api/platypusItems/42 will be mapped to GetItemById.

Note, that you need to enable attribute routing in configuration like this:

public static class WebApiConfig

{

public static void Register(HttpConfiguration config)

{

config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(

name: "DefaultApi",

routeTemplate: "api/{controller}/{id}",

defaults: new { id = RouteParameter.Optional }

);

}

}

But generally you should pass arguments as additional parameters. It is especially easy with GET requests. This will work in Web API 1&2:

public class ItemsController : ApiController

{

public string GetItemById(int id)

{

// Find item here ...

return item.ToString();

}

public string GetItemByNameAndId(string name, int id)

{

// Find item here ...

return item.ToString();

}

}

Assuming that you have default mapping configuration, http://192.112.183.42:80/api/platypusItems/42 will be mapped to GetItemById while http://192.112.183.42:80/api/platypusItems/42?name=DuckbilledPlatypisAreGuysToo will be mapped to GetItemByNameAndId because Web API can map 2 parameters instead of 1 for GetItemById.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值