0

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?

DarioN1
  • 2,460
  • 7
  • 32
  • 67

1 Answers1

1

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

taquion
  • 2,667
  • 2
  • 18
  • 29
  • 1
    I solved it by adding HTTPGET but I don't understand why it is now working... Its because the Route contains 'del' string and mvc do some checks on the route urls ? – DarioN1 May 17 '17 at 15:43
  • 1
    yes, and so it is mapping to [Route("user/del/{id:guid}")]. Web Api uses conventions when naming methods in your controllers. Because you named your method DeleteUser, Web Api expects Delete verb. – taquion May 17 '17 at 15:45
  • Thanks! thats why! can you integrate with this info the solution? I will mark as solved ass soon as I can – DarioN1 May 17 '17 at 15:46
  • 1
    I have just completed with latest info the answer. Best regards – taquion May 17 '17 at 15:49
  • 3
    Though the answer is definitely correct, I strongly suggest you to **not** use `GET` method for actions with side-effects like a deletion. [More info here](http://stackoverflow.com/a/786074/3670737). – Federico Dipuma May 17 '17 at 15:50
  • 1
    @DarioN Federuico is exactly right. anyone who browses that url with a correct user id will delete users. You definitely don't want a GET. At the least make it a POST – Fran May 17 '17 at 15:54