C# 使用webapi和angular资源删除

C# 使用webapi和angular资源删除,c#,angularjs,asp.net-web-api,angular-resource,C#,Angularjs,Asp.net Web Api,Angular Resource,我正在尝试使用angular resource en asp.net webapi删除集合中的项目。调用了正确的服务器端方法,但没有随它一起发送对象:行李箱为空 负责发送对象的代码中缺少了什么 角度代码: var myApp = angular.module('myApp', ['ui.router', 'ngResource']); myApp.config([ '$stateProvider', function($stateProvider) {

我正在尝试使用angular resource en asp.net webapi删除集合中的项目。调用了正确的服务器端方法,但没有随它一起发送对象:行李箱为空

负责发送对象的代码中缺少了什么

角度代码:

var myApp = angular.module('myApp', ['ui.router', 'ngResource']);

        myApp.config([
            '$stateProvider', function($stateProvider) {
                $stateProvider
                    .state('suitcases.delete', {
                        url: '/suitcase/delete/:id',
                        controller: 'deleteSuitcaseController'
                    });
            }
        ]);

angular.module('myApp').controller('deleteSuitcaseController', function ($scope, $state, $stateParams, Entry) {
            console.log('deleteSuitcaseController', $stateParams.id);

            $scope.deleteSuitcase = function () {
                var suitcase = Entry.get({ id: $stateParams.id });
                suitcase.$delete();
            };

            $scope.deleteSuitcase();
        });

angular.module('myApp').factory('Entry', function($resource) {

            return $resource(
                "api/suitcaseapi/:id", { id: '@@id' },
                { "update": { method: "PUT" } },
                { "add": { method: "POST" } }
            );
        });
webapi代码:

public class SuitcaseApiController : ApiController
    {
public void Delete(Suitcase suitcase)
        {
            Singleton.Instance.Suitcases.RemoveAll(p => p.Id == suitcase.Id);
        }
    }
如果我将服务器端方法更改为下面的代码,则在浏览器中会出现405不允许的错误

    public void Delete(int suitcaseid)
        {
            Singleton.Instance.Suitcases.RemoveAll(p => p.Id == suitcaseid);
        }
    }

另外,我取出了我认为不相关的代码。

如果是删除,并且我可以看到您已经拥有SuiteCaseID,您不需要调用API返回整个对象就可以删除它。只需传递Id,API就可以找到实体。大概是这样的:

角度控制器/功能

angular.module('myApp').controller('deleteSuitcaseController', function ($scope, $state, $stateParams, Entry) {

    $scope.entity.id = $stateParams.id;

    $scope.deleteSuitcase = function () {

        $http.delete('/Suitcase/' + $scope.entity.id)
        .success(function (data, status, headers) {

        })
        .error(function (data, status, header, config) {
        });
    }
});
删除方法API

public void Delete(int id)
{   
    Singleton.Instance.Suitcases.RemoveAll(p => p.Id == id);
}

条目在工厂中定义。请参阅更新的代码示例。由于未定义$http和$scope.entity,此示例不起作用。如果我解决了这个问题,我会得到一个methodnotallowed(405)错误。顺便说一句,当我将示例中的服务器端方法更改为publicvoiddelete(intid)时,也会遇到同样的错误。除此之外,这个解决方案没有使用资源库,可能是我还不清楚:我的目标是使用角度资源。我只提供了一个代码的示例。我不知道您正在使用哪些库或资源,所以我尝试创建一个通用代码,以便您能够想象这个想法。在你的问题中,你没有指明你得到了405个错误。。