0

I have written API with .NET Core 3.0 Preview, and using Angular 8 for frontend. I have one method on User controller called DeleteUser. This is the method:

//DELETE users/d18da620-6e4d-4b1c-a1ec-36d89145f4ca
[HttpDelete("{id}")]
public async Task<IActionResult> DeleteUser(Guid id) {
    ...
}

So, when request is performed with Angular like this:

this.usersApiService.deleteUser(userId).subscribe((data) => {
    ...
}

After this call is performed, I get following output in Visual Studio:

Microsoft.AspNetCore.Cors.Infrastructure.CorsService:Information: CORS policy execution successful.
Microsoft.AspNetCore.Routing.EndpointMiddleware:Information: Executing endpoint '405 HTTP Method Not Supported'

However, I've set custom CORS policy. In ConfigureServices I have this:

services.AddCors(options => {
    options.AddPolicy("CorsPolicy",
        builder => builder
            .AllowAnyMethod()
            .AllowCredentials()
            .SetIsOriginAllowed((host) => true)
            .AllowAnyHeader());
});

and in Configure method, I use this policy:

app.UseCors("CorsPolicy");

So my question is, how to successfuly call this delete method? What am I missing?

Edit: This is my deleteUser method that calls api

public deleteUser(userId) {
    const httpOptions = {
      headers: new HttpHeaders({
        'Content-Type' : 'application/json'
      }),
      body: {
        id: userId
      }
    };
    return this.httpClient.delete(this.USERSENDPOINT, httpOptions);
}
3
  • 1
    Can you provide usersApiService.deleteUser code? Commented Aug 24, 2019 at 8:55
  • @Nikita, oh yeah, I pasted wrong code there :) Thank you. Will add it to my question Commented Aug 24, 2019 at 9:14
  • By spec, you can't have a body in a DELETE call. The URL has to define the resource to delete. Commented Aug 24, 2019 at 17:31

1 Answer 1

1

Update: Try to call your method in angular like this:

this.httpClient.delete(this.USERSENDPOINT+"/"+userId);

hope it helps

Sign up to request clarification or add additional context in comments.

7 Comments

With this configuration, I get following error: HTTP Error 500.19 - Internal Server Error The configuration section 'httpProtocol' cannot be read because it is missing a section declaration
Nah, It wasnt inside system.webServer element. However, with this added to web.config, now I can't event perform basic Get on whole collection (example: GET /users
There is a error in JS console saying that CORS header Access-Control-Allow-Origin does not match localhost:4200
Where is the body in your delete request?
I believe that problem is in Angular not in .NET ... just occurred to me this now.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.