如何使用Angularjs控制器从ASP.NET ApiController调用特定的get方法?
问题描述:
我有包含多个GET方法的ApiController类。如何使用Angularjs控制器从ASP.NET ApiController调用特定的get方法?
//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调用特定的GET方法?
答
如果你有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