简体   繁体   English

从同一控制器路由多个GET方法-Web Api

[英]Routing multiple GET methods from same controller - Web Api

I'm currently having a problem with a Web Api I'm working on. 我目前正在使用的Web Api有问题。

I have a controller with two Get methods. 我有一个带有两个Get方法的控制器。 one which returns a list of objects. 一个返回对象列表的对象。 the other which returns a list of the same object, but filtered based on some parameters that are passed in. Like so: 另一个返回相同对象的列表,但根据传入的某些参数进行过滤。如下所示:

public IList<MyObject> Get(int id)
{
  //Code here looks up data, for that Id
}

public IList<MyObject> Get(int id, string filterData1, string filterData2)
{
  //code here looks up the same data, but filters it based on 'filterData1' and 'filterData2'
}

I cannot make the routes work for this. 我无法为此使用路线。 Especially as the Api help page seems to display the same url multiple times. 特别是因为Api帮助页面似乎多次显示相同的url。

my routes look like: 我的路线如下:

            config.Routes.MapHttpRoute(
            name: "FilterRoute",
            routeTemplate:  "api/Mycontroller/{Id}/{filterData1}/{filterData2}",
            defaults: new { controller = "Mycontroller" }
        );

        config.Routes.MapHttpRoute(
            name: "normalRoute",
            routeTemplate: "api/Mycontroller/{Id}",
            defaults: new { controller = "Mycontroller" }
        );

Anyone know? 有人知道吗

Also, is it possible to change my filtered method to something like 另外,是否可以将我的过滤方法更改为类似

public IList<MyObject> Get(int Id, FilterDataObject filterData)
{
   //code here
}

Or can you not pass complex objects on a Get? 还是不能在Get上传递复杂的对象?

Try looking at the attribute routing nuget package. 尝试查看属性路由 nuget包。 This allows you to define custom urls to each method in your controller. 这使您可以为控制器中的每个方法定义自定义URL。

With regards to your second question you can't send complex objects over get requests as there's no request body to hold the values, you will need to use a POST method to do this. 关于第二个问题,由于没有请求主体可保存值,因此无法通过get请求发送复杂的对象,您将需要使用POST方法来执行此操作。

Lets say you have the following route: 假设您有以下路线:

routes.MapHttpRoute(
    name: "Default", 
    routeTemplate: "api/{controller}/{id}/{p1}/{p2}",
    defaults: new { id = RouteParameter.Optional, p1 = RouteParameter.Optional, p2 = RouteParameter.Optional });

GET api/controller?p1=100 map to public HttpResponseMessage Get(int p1) {} GET api/controller?p1=100映射到public HttpResponseMessage Get(int p1) {}

GET api/controller/1?p1=100 map to public HttpResponseMessage Get(int id, int p1) {} GET api/controller/1?p1=100映射到public HttpResponseMessage Get(int id, int p1) {}

GET api/controller/1 map to public HttpResponseMessage Get(int id) {} GET api/controller/1映射到public HttpResponseMessage Get(int id) {}

and so on... 等等...

GET and complex model bind: by definition a complex model should be in the request body (verb independent) (a url contains a length limitation that can broke complex models). GET和复杂模型绑定:根据定义,复杂模型应位于请求正文中(独立于动词)(URL包含可能破坏复杂模型的长度限制)。 You can force the WebApi to look for the complex model in the URL by doing: 您可以通过以下方法强制WebApi在URL中查找复杂模型:

routes.MapHttpRoute(
    name: "Default", 
    routeTemplate: "api/{controller}/{customer}");

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public HttpResponseMessage Get([FromUri] Customer customer) {};

GET api/customers?id=1&name=Some+name

Just a note: GET with complex types, most of the time (like my example) makes no sense. 请注意:大多数情况下,使用复杂类型的GET(如我的示例)毫无意义。 Why should you get a customer by id and name? 为什么要通过ID和名称获得客户? By definition a complex type expects a POST (CREATE) or a PUT (UPDATE). 根据定义,复杂类型需要POST(CREATE)或PUT(UPDATE)。

To call with subfolders structure, try: 要使用子文件夹结构进行调用,请尝试:

routes.MapHttpRoute(
    "MyRoute",
    "api/{controller}/{id}/{p1}/{p2}",
    new { id = UrlParameter.Optional, p1 = UrlParameter.Optional, p2 = UrlParameter.Optional, Action = "Get"});

GET /api/controller/2134324/123213/31232312

public HttpResponseMessage Get(int id, int p1, int p2) {};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM