【发布时间】:2015-12-24 02:29:46
【问题描述】:
我有多个Web Api Post methods 喜欢:
[HttpPost]
public async Task<IHttpActionResult> Post([FromBody] AdModel adModel)
{
//Post
}
[HttpPost]
[Route("AddToWatchList")]
public async Task<IHttpActionResult> AddToWatchList(int adId)
{
// Post to database
}
这是我的 AngularJs 服务器:
app.factory('adService', ['$resource', 'ngAuthSettings', function ($resource, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
return $resource(serviceBase + 'api/Ads/', {}, {
addToWatchList: {
method: 'POST',
url: serviceBase + 'api/Ads/addToWatchList'
}
}
});
}]);
AngularJs 控制器:
app.controller('listCategoryAdsController',
['ngAuthSettings', '$scope', 'adService', '$routeParams', '$location',
function (ngAuthSettings, $scope, adService, $routeParams, $location) {
$scope.addToWatchlist = function (adId) {
adService.addToWatchList({ adId: adId }).$promise.then(
function(result) {
var path = $location.path();
$location.path(path).search('nw', 1);
},
function (error) {
});
};
我从中调用方法的 Html:
<div class="col-sm-3 text-right price-box" data-ng-repeat="ad in Ads">
<h2 class="item-price">
$ {{ad.price}}
</h2>
<i class="fa fa-certificate">
</i>
<a ng-click="addToWatchlist(ad.adId)" class="btn btn-default btn-sm make-favorite" tooltip="Click to add to watch list"
tooltip-placement="left"
tooltip-trigger="mouseenter">
<i class="fa fa-heart"></i>
<span>Watch list</span>
</a>
</div>
我的问题是 Post([FromBody] AdModel adModel) 被完美调用。但是当我打电话给AddToWatchList(int adId) 时,我得到了:
POST http://localhost:8081/api/Ads/addToWatchList 404(未找到)
【问题讨论】:
-
服务器 webapi 控制器上设置的基本路由是什么?你也可以将webapi函数重命名为
PostAddToWatchList并尝试。 -
基础路由是[RoutePrefix("api/Ads")]
标签: javascript .net angularjs asp.net-web-api angular-resource