2

我知道这个问题已经被问过很多次了。但我有一些不同的问题。

ASP.NET Web API 中具有多个 GET 方法的单个控制器

我已经按照上面链接的帖子中提供的步骤进行操作,但是我的 api 链接的格式为 api/servicename/controller。

帖子中给出的路由是 api/controller 格式。任何人都可以帮助解决这个问题。我实际上用我的服务的确切名称替换了路线,但没有用。

4

1 回答 1

2

我得到了解决方案,我没有将路由写成 api/controller/action,而是按照下面给出的方式编写了它。我从链接http://lonetechie.com/2013/03/04/fixing-multiple-actions-were-found-that-match-the-request-aspnet-webapi/获得了代码块,然后指向链接我已经在原帖中给出了。

config.Routes.MapHttpRoute("DefaultApiWithAction", "{controller}/{action}", new { action = "Get" });
config.Routes.MapHttpRoute("DefaultApi", "{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
config.Routes.MapHttpRoute("DefaultApiGet", "{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
config.Routes.MapHttpRoute("DefaultApiPost", "{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });
config.Routes.MapHttpRoute("DefaultApiPut", "{controller}", new { action = "Put" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Put) });
config.Routes.MapHttpRoute("DefaultApiDelete", "{controller}", new { action = "Delete" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Delete) });
于 2013-05-09T03:48:27.690 回答