具有多个Get操作的WEB API路由

时间:2013-09-27 05:42:45

标签: asp.net-web-api

我是WEB API的新手,并尝试遵循网站上设置路由的方法

在我的控制器中,我有所有基于标准动词的动作Get ...删除。

我还有其他一些基于Get动词的动作。我们为每个控制器注册路线,下面是我的路线设置。 见下文。我的Actions方法具有如下属性。但是当我输入例如http://server:49279/api/Licenses时 我得到错误说MultipleActions匹配请求    动作搜索,SearchByArtistId

当控制器硬编码时,不确定如何处理此问题

任何帮助都很有价值,因为我无法在线找到解决方案

由于

[System.Web.Http.AcceptVerbs(new string[] { "Get" })]
[System.Web.Http.ActionName("PublishersByLicense")]
public HttpResponseMessage PublishersByLicense(Guid Id)
{
string vControllerName="Licenses";
config.Routes.MapHttpRoute
       (
           name: "PublishersByLicense" + vControllerName,
           routeTemplate: "api/" + vControllerName + "/" + "PublishersByLicense" + "/{aLicenseId}",
           defaults: new
           {
               controller = vControllerName,
               action = "PublishersByLicense"
           },
           constraints: new
           {
               httpMethod = new HttpMethodConstraint(HttpMethod.Get)
           }
        );


            config.Routes.MapHttpRoute
           (
               name: "Search" + vControllerName,
               routeTemplate: "api/" + vControllerName + "/" + "Search" + "/{aTrackIds}",
               defaults: new
               {
                   controller = vControllerName,
                   action = "Search"
               },
               constraints: new
               {
                   httpMethod = new HttpMethodConstraint(HttpMethod.Get)
               }
           );



            config.Routes.MapHttpRoute
            (
                name: "GetAll" + vControllerName,
                routeTemplate: "api/" + vControllerName,
                defaults: new
                {
                    controller = vControllerName
                },
                  constraints: new
                  {
                      httpMethod = new HttpMethodConstraint(HttpMethod.Get)
                  }
            );


            config.Routes.MapHttpRoute
           (
               name: "GetSingle" + vControllerName,
               routeTemplate: "api/" + vControllerName + "/{aLicenseId}",
               defaults: new
               {
                   controller = vControllerName
               },
               constraints: new
               {
                   httpMethod = new HttpMethodConstraint(HttpMethod.Get)
               }
           );

这是我的控制器代码

[System.Web.Http.AcceptVerbs(new string[] { "Get" })]
[System.Web.Http.ActionName("Search")]
public HttpResponseMessage Search([FromUri]Guid[] aTrackIds)
{
public HttpResponseMessage Get()
{

1 个答案:

答案 0 :(得分:0)

http://server:49279/api/Licenses的请求将与您定义的第三条路线匹配:

config.Routes.MapHttpRoute
(
    name: "GetAll" + vControllerName,
    routeTemplate: "api/" + vControllerName,
    defaults: new
    {
        controller = vControllerName
    },
    constraints: new
    {
        httpMethod = new HttpMethodConstraint(HttpMethod.Get)
    }
);

由于您没有在其中明确指定action路由值,因此Web API会尝试找到最适合的操作。

我不知道您的控制器代码,但我认为SearchSearchByArtistId具有相同数量的参数,可以从请求数据填充,从而使它们同样好(或行动选择。这就是Web API无法明确选择正确行动的原因。