【问题标题】:Routing multiple GET methods from same controller - Web Api从同一个控制器路由多个 GET 方法 - Web Api
【发布时间】:2013-10-16 15:54:54
【问题描述】:

我目前正在处理的 Web Api 有问题。

我有一个带有两个 Get 方法的控制器。一个返回对象列表的方法。另一个返回相同对象的列表,但根据传入的一些参数进行过滤。像这样:

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

我无法让路线为此工作。尤其是 Api 帮助页面似乎多次显示相同的 url。

我的路线如下:

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

有人知道吗?

另外,是否可以将我的过滤方法更改为类似

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

或者你不能在 Get 上传递复杂的对象吗?

【问题讨论】:

  • 你在尝试什么网址?
  • server/api/MyController/1/"someString"/"someOtherString" 和 server/api/Mycontroller/1
  • 您尝试的 URL 与您正在创建的路由不符(查看我的回复)。

标签: c# asp.net-web-api asp.net-web-api-routing


【解决方案1】:

假设您有以下路线:

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 映射到public HttpResponseMessage Get(int p1) {}

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

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

等等……

GET 和复杂模型绑定:根据定义,复杂模型应该在请求正文中(与动词无关)(url 包含可能破坏复杂模型的长度限制)。您可以通过执行以下操作强制 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

请注意:大多数时候(如我的示例)使用复杂类型的 GET 是没有意义的。为什么要通过 id 和 name 获得客户?根据定义,复杂类型需要 POST (CREATE) 或 PUT (UPDATE)。

要使用子文件夹结构调用,请尝试:

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

【讨论】:

  • 谢谢。我完全同意这应该是一个帖子的事实。事实上,这是一个帖子,但权力不同意,所以我在这里。至于路由,我明白你在说什么。但是,我遇到的问题是我正在使用将文档(帮助页面)添加到 Api 的 nuGet 包。它定义的路由以您描述的格式为我提供了 Url,例如 'api/controller/1?p1=p1' 但我想将它们显式显示为参数,所以 api/controller/1/parameter1
  • 谢谢,带有可选参数的路由有效。赞成。我仍然认为这是解决此问题的错误方法,并且发布过滤器数据的对象是更好的解决方案。但这暂时可以!
【解决方案2】:

尝试查看attribute routing nuget 包。这允许您为控制器中的每个方法定义自定义 url。

关于您的第二个问题,您不能通过 get 请求发送复杂对象,因为没有请求正文来保存值,您需要使用 POST 方法来执行此操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    相关资源
    最近更新 更多