简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM