C# 如何将多个参数传递/接收到 RESTful Web API GET 方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19891494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:03:56  来源:igfitidea点击:

How to pass/receive multiple args to a RESTful Web API GET method?

c#restasp.net-web-apiasp.net-4.5asp.net-web-api-routing

提问by B. Clay Shannon

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

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

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.

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

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?

IOW,路由机制是否会发现,由于传递了两个 args,它应该使用两个 args 调用 Get() 方法(“约定优于配置”),还是需要做更多工作才能适当地路由事物?

采纳答案by Nikolai Samteladze

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

如果您使用 Web API 2,那么您可以使用属性路由来路由请求,例如 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/42will be mapped to GetItemByNameAndIdwhile http://192.112.183.42:80/api/platypusItems/42will be mapped to GetItemById.

http://192.112.183.42:80/api/platypusItems/DuckbilledPlatypisAreGuysToo/42将被映射到GetItemByNameAndIdhttp://192.112.183.42:80/api/platypusItems/42将被映射到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:

但通常您应该将参数作为附加参数传递。使用 GET 请求尤其容易。这将适用于 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/42will be mapped to GetItemByIdwhile http://192.112.183.42:80/api/platypusItems/42?name=DuckbilledPlatypisAreGuysToowill be mapped to GetItemByNameAndIdbecause Web API can map 2 parameters instead of 1 for GetItemById.

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

More information can be found in Mike Wasson articles on Attribute Routing, Routing and Action Selectionand Routing in Web API.

更多信息可以在 Mike Wasson 关于属性路由路由和操作选择以及Web API 中的路由的文章中找到。