I am trying to pass id value from angular code to asp.net web api. I tried couple of ways but still unable to pass the value. What may be the code issue?
我试图将id值从角度代码传递到asp.net web api。我尝试了几种方法,但仍然无法传递值。代码问题是什么?
Angular Service code
角服务代码
angular.module('myServiceApp')
.factory('mySvc', ['$http', function ($http) {
return {
startJob: function (id) {
return $http.put('/api/Job/Put/' + id);
}
};
}]);
HTML Code
HTML代码
<div ng-click="initiateJob(id)">
Start Now
</div>
Angular Function
角函数
$scope.initiateJob = function (id) {
mySvc.startJob(id)
.success(function () {
alert('started successfully');
}).error(function (err) {
$scope.error = err;
});
}
webapi code
之前的代码
public HttpResponseMessage Put(int id)
{
HttpResponseMessage response = null;
return response;
}
1 个解决方案
#1
2
Try changing this line:
试着改变这条线:
return $http.put('/api/Job/Put/' + id);
Into this:
到这个:
return $http.put('/api/Job/' + id);
I did this on my side and it works just fine, here's a complete example:
我在我这边做了这个,效果很好,这里有一个完整的例子:
WEB API Controller:
WEB API控制器:
public class JobController : ApiController
{
public HttpResponseMessage Put(int id)
{
HttpResponseMessage response = null;
return response;
}
}
View:
观点:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js' type="text/javascript"></script>
<script type="text/javascript">
angular.module('app', []);
angular.module("app").controller('mainController', function ($scope, $http, $compile) {
$scope.CallPut = function () {
debugger
var id = 1;
$http.put("/api/Job/" + id).
success(function (data, status, headers, config) {
debugger;
}).
error(function (data, status, headers, config) {
debugger;
});
}
});
</script>
<div ng-app="app">
<div id="mainController" ng-controller="mainController">
<input type="button" ng-click="CallPut()" value="Call Put" />
</div>
</div>
Also make sure you have a file called WebApiConfig.cs in the App_Start folder of your project where you define web api routes and makes sure you reister your web api routes BEFORE the MVC routes in the Global.asax file:
还要确保有一个名为WebApiConfig的文件。在您的项目的App_Start文件夹中,您定义了web api路由,并确保您在全球的MVC路由之前对您的web api路由进行了分析。asax文件:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
#1
2
Try changing this line:
试着改变这条线:
return $http.put('/api/Job/Put/' + id);
Into this:
到这个:
return $http.put('/api/Job/' + id);
I did this on my side and it works just fine, here's a complete example:
我在我这边做了这个,效果很好,这里有一个完整的例子:
WEB API Controller:
WEB API控制器:
public class JobController : ApiController
{
public HttpResponseMessage Put(int id)
{
HttpResponseMessage response = null;
return response;
}
}
View:
观点:
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js' type="text/javascript"></script>
<script type="text/javascript">
angular.module('app', []);
angular.module("app").controller('mainController', function ($scope, $http, $compile) {
$scope.CallPut = function () {
debugger
var id = 1;
$http.put("/api/Job/" + id).
success(function (data, status, headers, config) {
debugger;
}).
error(function (data, status, headers, config) {
debugger;
});
}
});
</script>
<div ng-app="app">
<div id="mainController" ng-controller="mainController">
<input type="button" ng-click="CallPut()" value="Call Put" />
</div>
</div>
Also make sure you have a file called WebApiConfig.cs in the App_Start folder of your project where you define web api routes and makes sure you reister your web api routes BEFORE the MVC routes in the Global.asax file:
还要确保有一个名为WebApiConfig的文件。在您的项目的App_Start文件夹中,您定义了web api路由,并确保您在全球的MVC路由之前对您的web api路由进行了分析。asax文件:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}