[英]c# webapi - The requested resource does not support http method 'GET'
I have two simple routing methods: 我有两种简单的路由方法:
[RoutePrefix("api/accounts")]
[Route("user/{id:guid}", Name = "GetUserById")]
public async Task<IHttpActionResult> GetUser(string Id)
[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: 我想知道为什么我要用直接ulr(GET)测试第一种方法是否有效:
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'. 我得到: 请求的资源不支持http方法'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.
实际上,它指向您为delete方法定义的路由。 This method expects DELETE Http verb.
此方法需要DELETE Http动词。
When you write the url directly on the browser it performs a GET request. 当您直接在浏览器中写入url时,它将执行GET请求。 If you want your delete method to work with a GET verb you have to put the attribte
如果要让delete方法与GET动词配合使用,则必须添加属性
[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
您可以使用Fiddler或Postman之类的HTTP客户端进行测试
Web Api uses conventions when naming methods in your controllers. Web Api在控制器中命名方法时使用约定。 Because you named your method DeleteUser, Web Api expects Delete verb.
因为您将方法命名为DeleteUser,所以Web Api希望使用Delete动词。
EDIT>>>Please follow recommendations listed in comments. 编辑>>>,请遵循注释中列出的建议。 I have also make bold the part where I do not recommend this
我也将不推荐的部分加粗
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.