我有多个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我正在调用meth OD:
<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并尝试。 – Chandermani
基本路线是[RoutePrefix(“api/Ads”)] –