typescript 在Angular和.NET的Web API中删除HttpDelete

yxyvkwin  于 2023-01-03  发布在  TypeScript
关注(0)|答案(1)|浏览(110)

我的问题是http.delete在服务器端没有命中http delete。
Angular 法:

public deleteActivity(id: number, activity: string){
    const url = `https://localhost:5001/api/calendar/${activity}/${id}`
    return this.http.delete(url);
  }

C#控制器:

[HttpDelete("{id}")]
    public ActionResult Delete([FromRoute] int id)
    {
        _trainingService.Delete(id);

        return NoContent();
    }

没有得到任何错误,这里我的http客户端处理程序:

{
    "backend": {
        "xhrFactory": {}
    },
    "injector": {
        "parent": {
            "parent": {
                "parent": {},
                "source": "Platform: core",
                "scopes": {},
                "records": {},
                "_ngOnDestroyHooks": {},
                "_onDestroyHooks": [],
                "_destroyed": false,
                "injectorDefTypes": {}
            },
            "source": "AppModule",
            "scopes": {},
            "records": {},
            "_ngOnDestroyHooks": {},
            "_onDestroyHooks": [],
            "_destroyed": false,
            "injectorDefTypes": {}
        },
        "source": "AppModule",
        "scopes": {},
        "records": {},
        "_ngOnDestroyHooks": {},
        "_onDestroyHooks": [],
        "_destroyed": false,
        "injectorDefTypes": {}
    },
    "chain": {
        "next": {
            "next": {
                "xhrFactory": {}
            },
            "interceptor": {
                "jwtHelper": {},
                "document": {
                    "location": {
                        "ancestorOrigins": {},
                        "href": "http://localhost:4200/calendar",
                        "origin": "http://localhost:4200",
                        "protocol": "http:",
                        "host": "localhost:4200",
                        "hostname": "localhost",
                        "port": "4200",
                        "pathname": "/calendar",
                        "search": "",
                        "hash": ""
                    }
                },
                "standardPorts": [
                    "80",
                    "443"
                ],
                "headerName": "Authorization",
                "authScheme": "Bearer ",
                "allowedDomains": [
                    "localhost:5001"
                ],
                "disallowedRoutes": [],
                "throwNoTokenError": false
            }
        },
        "interceptor": {
            "tokenService": {
                "doc": {
                    "location": {
                        "ancestorOrigins": {},
                        "href": "http://localhost:4200/calendar",
                        "origin": "http://localhost:4200",
                        "protocol": "http:",
                        "host": "localhost:4200",
                        "hostname": "localhost",
                        "port": "4200",
                        "pathname": "/calendar",
                        "search": "",
                        "hash": ""
                    }
                },
                "platform": "browser",
                "cookieName": "XSRF-TOKEN",
                "lastCookieString": "",
                "lastToken": null,
                "parseCount": 0
            },
            "headerName": "X-XSRF-TOKEN"
        }
    }
}

如果我从断点here复制url并把它放到postman中,它会正常工作并正确删除训练。这是我的Angular 方法中的一些语法错误吗?

xtupzzrd

xtupzzrd1#

你必须订阅可观察对象才能使其执行

deleteActivity(1,"training").subscribe(() => console.log('Delete successful'));

http.delete()返回类型是可观察的,且如果没有订阅者,则可观察的不解析

相关问题