0

Web API に 2 つ以上の get メソッドがあります (以下のように、コード GetProducts と GetProductsNew は get メソッドです)。angular jsのng-resourceを使用しているため、get関数を呼び出そうとすると、あいまいなエラーが発生します。GetProductsNew メソッドを呼び出したいとしたら、どうすれば呼び出せますか? 誰でも助けてください、事前に感謝します:)

    // This is my web api code
    public class ProductController : ApiController
    {
      ...
      public HttpResponseMessage GetProducts()
      { 
      }

      public HttpResponseMessage GetProductsNew()
      {
      }
    }

    // **This is my angular** 

    // This is the code in controller.js, using which i am calling the get method from above code, but here the problem is, since the above web api is having the two gt method, i am getting the "Multiple actions were found that match the request"

    productcatControllers.controller('ProductListCtrl', ['$scope', '$routeParams',
    '$location', '$route', 'productService',
    function ($scope, $routeParams, $location, $route, productService) 
    {
        productService.query(function (data) {
            $scope.products = data;
        });
    }]);

   // This is the service of angular js
   var phonecatServices = angular.module('productcatServices', ['ngResource']);

   phonecatServices.factory("productService", function ($resource) {
       return $resource(
           "/api/Product/:id",
           { id: "@id" },
           {
              "update": { method: "PUT" }

           }
       );
   });
4

1 に答える 1

0

「あいまいなエラー」によって、「リクエストに一致する複数のアクションが見つかりました....」というメッセージが表示されていると思います。Getこれは、同じコントローラーに同じ署名を持つ複数のメソッドを含めることができないためです。

これらの属性を webapi のメソッドに追加してみてください

public class ProductController : ApiController
{
  [HttpGet]
  [Route(Name = "GetProducts")]
  public HttpResponseMessage GetProducts()
  { 
  }

  [HttpGet]
  [Route(Name = "GetProductsNew")]
  public HttpResponseMessage GetProductsNew()
  {
  }
}

AspNet.WebApi.WebHostをまだインストールしていない場合は、インストールする必要があります。

Install-Package Microsoft.AspNet.WebApi.WebHost


この記事には、属性ルーティングに関する優れた説明があります

于 2016-02-20T08:17:56.153 に答える