DarioN1
DarioN1

Reputation: 2552

c# webapi - The requested resource does not support http method 'GET'

I have two simple routing methods:

main routing directive:

[RoutePrefix("api/accounts")]

first one

[Route("user/{id:guid}", Name = "GetUserById")]
public async Task<IHttpActionResult> GetUser(string Id)

second one

[Route("user/del/{id:guid}")]
public async Task<IHttpActionResult> DeleteUser(string id)

I wonder why if I test the first method with direct ulr ( GET ) it works:

http://localhost/ReportApp_WebApi/api/accounts/user/1533882b-e1fd-4b52-aeed-4799edbbcda6

And if I try the second link that is just a little different:

http://localhost/ReportApp_WebApi/api/accounts/user/del/1533882b-e1fd-4b52-aeed-4799edbbcda6

I get: The requested resource does not support http method 'GET'.

Can you help me?

Upvotes: 0

Views: 1732

Answers (1)

taquion
taquion

Reputation: 2767

The second link is not just a little different. In fact it is pointing to the route that you have defined for your delete method. This method expects DELETE Http verb.

When you write the url directly on the browser it performs a GET request. If you want your delete method to work with a GET verb you have to put the attribte

[HttpGet]

before or just after your Route atttribute. Although this I would not recommend to do it. You can have a HTTP client like Fiddler or Postman to test this

Web Api uses conventions when naming methods in your controllers. Because you named your method DeleteUser, Web Api expects Delete verb.

EDIT>>>Please follow recommendations listed in comments. I have also make bold the part where I do not recommend this

Upvotes: 1

Related Questions