4

似乎有一千人在堆栈溢出问题上提出相同的问题,但似乎没有一个单一的解决方案来解决这个问题。我又要问了。。。

我有一个具有以下操作的 API 控制器:

    // GET api/Exploitation
    public HttpResponseMessage Get() {
        var items = _exploitationRepository.FindAll();

        var mappedItems = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(items);

        var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItems);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { }));
        return response;
    }

    // GET api/Exploitation/5        
    [HttpGet, ActionName("Get")]
    public HttpResponseMessage Get(int id) {
        var item = _exploitationRepository.FindById(id);
        var mappedItem = Mapper.Map<Exploitation, ExploitationView>(item);

        var response = Request.CreateResponse<ExploitationView>(HttpStatusCode.OK, mappedItem);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id }));
        return response;
    }

    // GET api/Exploitation/GetBySongwriterId/5
    [HttpGet, ActionName("GetBySongwriterId")]
    public HttpResponseMessage GetBySongwriterId(int id) {
        var item = _exploitationRepository.Find(e => e.Song.SongWriterSongs.Any(s => s.SongWriterId == id))
                                          .OrderByDescending(e => e.ReleaseDate);
        var mappedItem = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(item);

        var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItem);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id }));
        return response;
    }

    // GET api/Exploitation/GetBySongwriterId/5
    [HttpGet, ActionName("GetBySongId")]
    public HttpResponseMessage GetBySongId(int id) {
        var item = _exploitationRepository.Find(e => e.SongId == id)
                                          .OrderByDescending(e => e.ReleaseDate);
        var mappedItem = Mapper.Map<IEnumerable<Exploitation>, IEnumerable<ExploitationView>>(item);

        var response = Request.CreateResponse<IEnumerable<ExploitationView>>(HttpStatusCode.OK, mappedItem);
        response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = id }));
        return response;
    }

在我的 APIConfig 中,我定义了以下路线:

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional },
            constraints: new { id = @"\d+" }
        );

我发现我可以毫无问题地访问以下操作: /api/exploitation /api/exploitation/getbysongwriterid/1 /api/exploitation/getbysongid/1

当我尝试访问 /api/exploitation/1 我得到这个异常

"Multiple actions were found that match the request: System.Net.Http.HttpResponseMessage Get(Int32) on type Songistry.API.ExploitationController System.Net.Http.HttpResponseMessage GetBySongwriterId(Int32)" exception.

谁能看到我的路线有什么问题?还是有其他问题?

4

3 回答 3

6

我找到了一个优雅的解决方案。

我修改了我的 ApiRouteConfig 以具有以下路由:

        config.Routes.MapHttpRoute(
            name: "DefaultGetApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional, action = "Get" },
            constraints: new { id = @"\d+", httpMethod = new HttpMethodConstraint(HttpMethod.Get) }
        );

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional },
            constraints: new { id = @"\d+" }
        );            

        config.Routes.MapHttpRoute(
            name: "ActionApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional, action = RouteParameter.Optional }
        );

现在我可以访问:

/api/exploitation
/api/exploitation/1
/api/exploitation/getbysongid/1
/api/exploitation/getbysongwriterid/1

我根本不需要修改我的控制器操作来使用这个新的路由配置。

如果您有多个 PUT 或 POST 操作,您可以创建如下所示的新路由:

    config.Routes.MapHttpRoute(
        name: "DefaultGetApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional, action = "Put" },
        constraints: new { id = @"\d+", httpMethod = new HttpMethodConstraint(HttpMethod.Put) }
    );

    config.Routes.MapHttpRoute(
        name: "DefaultGetApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional, action = "Delete" },
        constraints: new { id = @"\d+", httpMethod = new HttpMethodConstraint(HttpMethod.Delete) }
    );

我希望这个答案对每个人都有帮助,因为这似乎是人们遇到的一个普遍问题。

于 2013-03-09T17:56:04.090 回答
0

您遇到的问题是/api/exploitation/1属于:

    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

您的所有 GET 方法也都满足该路由,特别是因为{id}是可选的并且它们的控制器是相同的。

因此,您有一个来自客户端的 HTTP GET 请求和多个接受 GET 请求的方法。它不知道该去哪一个。

api/{controller}/{action}/{id} 
//This works fine because you specified which action explicitly

我希望这能回答你的问题。

于 2013-03-09T06:42:48.940 回答
0

在您的路线定义中尝试以下操作。仅保留以下路线:

  config.Routes.MapHttpRoute(
        name: "ActionApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional, action = "Get" },
        constraints: new { id = @"\d+" }
    );

将第一个 Get 方法设为私有,修改第二个使 id 具有默认值:

// GET api/Exploitation
private HttpResponseMessage Get() {
    // implementation stays the same but now it's private
}

// GET api/Exploitation/5        
[HttpGet, ActionName("Get")]
public HttpResponseMessage Get(int id = 0) {
    if (id == 0) {
        return Get();
    }

    // continue standard implementation
}

这样(我自己没有测试过)我希望:

  • api/Exploitation/ 将映射到 api/Exploitation/Get(作为默认操作),其中 id = 0 作为默认参数
  • api/Exploitation/1 将映射到 api/Exploitation/Get/1 因此它将调用 Get(1)
  • api/Exploitation/someOtherAction/345 将调用正确的操作方法

这可能会奏效。更严格的路由定义实际上可能是这样的:

  config.Routes.MapHttpRoute(
        name: "ApiWithRequiredId",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: null /* make sure we have explicit action and id */,
        constraints: new { id = @"\d+" }
    );

  config.Routes.MapHttpRoute(
        name: "ApiWithOptionalId",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional, action = "Get" },
        constraints: new { action = "Get" /* only allow Get method to work with an optional id */, id = @"\d+" }
    );

但是沿着这些思路......试一试,我希望它能解决你的问题。

于 2013-03-09T13:28:18.113 回答