Home > Net >  HttpDelete in web api in Angular and .NET
HttpDelete in web api in Angular and .NET

Time:12-31

my problem is that http.delete doesn't hit http delete on the server side.

Angular method:

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

C# controller:

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

        return NoContent();
    }

not getting any error, here mine http client - handler:

{
    "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"
        }
    }
}

If i copy url from breakpoint here and put him in postman it works and delete training correctly. it`s some syntax failure in my angular method?

CodePudding user response:

you have to subscribe on the observable to make it execute

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

the return type of http.delete() is an observable and observable does not resolve if there is no one subscribe to it

  • Related