如何将多个参数传递/接收到RESTful Web API GET方法? [英] How to pass/receive multiple args to a RESTful Web API GET method?

查看:1326
本文介绍了如何将多个参数传递/接收到RESTful Web API GET方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

采用参数(返回标量值而不是数据集)的GET RESTful方法的常见示例如下所示:

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
}

...其中传递的val通常是一个ID,因此您可以使用它来基于该唯一值获取标量值.

...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
}

...并这样称呼:

//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,路由机制会指出,由于传递了两个参数,因此应该使用两个参数调用Get()方法(约定之上的配置"),否则还有更多要做的事情合适吗?

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?

推荐答案

如果使用Web API 2,则可以使用属性路由来路由http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42

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将映射到GetItemByNameAndId,而http://192.112.183.42:80/api/platypusItems/42将映射到GetItemById.

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 }
        );
    }
}

但是通常您应该将参数作为附加参数传递.使用GET请求特别容易.这将在Web API 1& 2中​​起作用:

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();
    }
}

假定您具有默认的映射配置,因为Web API可以映射2个参数而不是1个c4,所以http://192.112.183.42:80/api/platypusItems/42将被映射到GetItemById,而http://192.112.183.42:80/api/platypusItems/42?name=DuckbilledPlatypisAreGuysToo将被映射到GetItemByNameAndId.

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.

更多信息可以在 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆