0

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

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

我还有其他一些基于 Get Verb 的操作。我们为每个控制器注册路由,下面是我的路由设置。见下文。我的 Actions 方法具有如下属性。但是,当我输入例如时,http://server:49279/api/Licenses 我收到错误消息 MultipleActions match request The Actions Search,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()
{
4

1 回答 1

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 会尝试自行查找最合适的操作。

我不知道您的控制器代码,但我认为两者都Search具有SearchByArtistId相同数量的参数,可以从请求数据中填充,从而使它们成为同样好的(或坏的)动作选择。这就是为什么 Web API 不能明确地选择一个正确的操作。

于 2013-09-27T07:23:39.800 回答