如何使用AngularJS控制器从ASP.NET apicontroller调用特定的获取方法



我有包括多个获取方法的apicontroller类。

    //method 1   
    public IEnumerable<Drive> GetProducts()
    {
                  ...
    }
    //method2
    public IEnumerable<string> GetCustomer(string name)
    {
                  ...
    }
    //method3
    public IEnumerable<string> GetCustomers()
    {
                  ...
    }

这是我的angularjs脚本

var app = angular.module("MyApp", []);
app.controller("MyController", function ($scope, $http) {
    $http.get('http://localhost:53551/api/values/GetProducts').
        success(function (data, status, headers, config) {
            $scope.strings = data;
        }).
        error(function (data, status, headers, config) {
            alert("sa");
        });
    debugger;
    $scope.open = function (name) {
        debugger;
        $http.get('http://localhost:53551/api/values/GetCustomer?' + name ).
            success(function (data, status, headers, config) {
                $scope.strings = data;
            })
    };
    });

所有函数仅调用getProducts()。但是我想每次都称为特定的。是否可以通过apicontroler的angularJS调用特定的获取方法?

如果已注册为DefaultWebapi路由为

routes.MapHttpRoute(
    name: "API Default",
    routeTemplate: "api/{controller}/{id}",
    defaults: new { id = RouteParameter.Optional }
);

然后,您只需要调用方法名称/api/controllerName/actionName(不需要在操作名称中提及HttpVerb)。每个动作名称之前的前缀表示动作的性质。我以为您有上述所有动作的ValuesController

http://localhost:53551/api/values/customer
http://localhost:53551/api/values/products
http://localhost:53551/api/values/customers

最新更新