ngResource.Delete()像对象一样解析我的Guid,而不是将其作为URL的一部分传递

时间:2015-04-10 15:51:54

标签: angularjs asp.net-web-api typescript ngresource

我正在使用TypeScript为我的Asp.NET.Web.API / MVC应用程序生成由ngresources支持的angularjs。我有一个仪表板,我用它来以RESTful方式为这个应用程序启用用户管理。我目前CRU部分的CRUD操作工作得很好。但是,DELETE功能已被破坏。

我正在尝试将我的成员的Identity值用作delete函数的参数。这就像我的PUT动词的魅力一样,所以令人困惑的是为什么DELETE动词导致GUID被破坏。

我的端点定义为/ api / member / {identity}。

每当angularJS尝试删除用户时,我都会得到:

DELETE https://localhost:44300/api/member?0=0&1=a&10=7&11=3&12=c&13=-&14=4&15=8&16…e&27=b&28=2&29=e&3=b&30=5&31=0&32=e&33=4&34=0&35=9&4=a&5=d&6=e&7=d&8=-&9=d 405 (Method Not Allowed)

我的MemberResourceBuilder:

public getResource(): Interfaces.IMemberResource {
        return <Interfaces.IMemberResource> this.$resource(this.baseUrl + "member/:identity", { identity: "@Identity" }, {
            update: this.updateAction});
    }

我的会员控制器:

module Controllers {
export class MembersController implements Interfaces.IMembersController {
    static id = "membersController";

    static $inject = ['$scope', '$rootScope', 'memberDataStore', 'editorDataStore', 'membersService'];

    constructor(private $scope: any, $rootScope: any, private memberDataStore: Interfaces.IMemberResource, private editorDataStore : Interfaces.IMemberResource, private membersService: Services.MembersService) {
        if (!angular.isDefined(memberDataStore)) {
            Ntier.Tools.logError("MemberDataStore NOT defined.  Controller not properly constructed.", "Error initializing member functions.  See debug console for details.");
        } else {
            //$scope.members = memberDataStore.query();
            this.loadMembers();
        }
        $scope.actions = this;
    }

    newMember : Interfaces.IMember;

    loadMembers() {
        this.$scope.members = this.memberDataStore.query();
        if (!angular.isDefined(this.$scope.members)) {
            Ntier.Tools.logError("Members NOT defined.  Controller not properly constructed.", "Error initializing members.  See debug console for details.");
        }
    }

    selectMember(member: Interfaces.IMember) {
        this.membersService.selectMember(member);
        this.$scope.selectedMember = this.membersService.selectedMember;
    }

    update(member: Interfaces.IMember) {
        alert('saving');
        this.editorDataStore.update(member,(updatedMember: Interfaces.IMember) => {
            this.loadMembers();
        }, (reason:any)=>{alert(reason)});
    }

    save(member: Interfaces.IMember) {
        this.memberDataStore.save(member, (newMember: Interfaces.IMember) => {
            this.loadMembers();
            this.$scope.actions.newMember = <Interfaces.IMember> new Entities.Member();
        }, (reason: any) => { alert(reason) });
    }

    delete(member: Interfaces.IMember) {
        alert(member.Identity);
        this.memberDataStore.delete(member.Identity, () => {
            this.loadMembers();
            this.$scope.actions.newMember = <Interfaces.IMember> new Entities.Member();
        }, (reason: any) => { alert('failed to Delete the user') });
    }
}

我的IMember:

// ReSharper disable InconsistentNaming

模块接口{

export interface IMember extends ng.resource.IResource<IMember> {
    UserName: string;
    Password: string;
    ConfirmedPassword: string;
    IsApproved: boolean;
    FirstName: string;
    Middle: string;
    LastName: string;
    FullName: string;

    Creating: boolean;
    Viewing: boolean;
    Editing: boolean;
    Deleting: boolean;
    ChangePassword: boolean;
    Identity: string;


}

}

我的IMemberResource:

module Interfaces {
export interface IMemberResource extends ng.resource.IResourceClass<IMember> {
    update(member: IMember): IMember;
    update(member: IMember, success?: (member: IMember) => void, error?: (reason: any) => void): IMember;

}

}

更新

<张>张志敏的钱是正确的。我很确定我必须传递一个对象,但我的问题是区分大小写。志敏的答案奏效了,但我能够把他的答案变成我认为对我正在使用的物品更真实的东西。

在控制器中:

delete(member: Interfaces.IMember) {
        this.memberDataStore.delete(member, () => {
            this.loadMembers();
            this.$scope.actions.newMember = <Interfaces.IMember> new Entities.Member();
        }, (reason: any) => {
                alert('Failed to Delete the user!  Reason: ' + reason);
        });

    }

然后在资源构建器中:

        public getResource(): Interfaces.IMemberResource {
        return <Interfaces.IMemberResource> this.$resource(this.baseUrl + "member/:Identity", { Identity: "@Identity" }, {
            update: this.updateAction});
    }

请注意使用大写而不是小写。

1 个答案:

答案 0 :(得分:1)

您传递给delete的参数应该是一个对象,而不仅仅是一个标识:

使用

this.memberDataStore.delete({ identity: member.Identity}, () => {})

而不是

this.memberDataStore.delete(member.Identity, () =>{})