简体   繁体   English

c#webapi-请求的资源不支持http方法'GET'

[英]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: 我想知道为什么我要用直接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.

相关问题 WebApi-请求的资源不支持http方法“ GET” - WebApi - The requested resource does not support http method 'GET' C#所请求的资源不支持http方法补丁 - C# The requested resource does not support http method patch WebApi“请求的资源不支持http方法&#39;DELETE” - WebApi “The requested resource does not support http method 'DELETE” 请求的资源不支持HTTP方法GET - The requested resource does not support HTTP method GET 请求的资源不支持 HTTP 方法“GET” - The requested resource does not support HTTP method 'GET' WebApi Post 方法总是返回“请求的资源不支持 http 方法 &#39;GET&#39;。” 状态:405 方法不允许 - WebApi Post Methods always Returns “The requested resource does not support http method 'GET'.” Status: 405 Method Not Allowed WebApi 2 Http Post 405“请求的资源不支持http方法&#39;POST&#39;” - WebApi 2 Http Post 405 “The requested resource does not support http method 'POST'” 所请求的资源在Visual Studio 2010中不支持HTTP方法“ GET” - The requested resource does not support HTTP method 'GET' in visual studio 2010 请求的资源不支持 http 方法“GET”,但使用“POST” - The requested resource does not support http method 'GET' but using 'POST' ASP.Net-请求的资源不支持http方法“ GET” - ASP.Net - Requested resource does not support http method 'GET'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM