gpt4 book ai didi

c# - 为什么 http get 方法在 asp.net web api 中接受 http post 请求?

转载 作者:太空宇宙 更新时间:2023-11-03 20:50:39 26 4
gpt4 key购买 nike

我有一个 HTTP-GET 方法,如下所示

[Route("api/[controller]")]
[ApiController]
public class CityController : ControllerBase
{
public ActionResult Get(int id)
{
try
{
var city = new { CityName = "Gotham" };
return Ok(city);
}
catch(Exception ex)
{
return StatusCode(500);
}
}
}

对于这两种类型的请求

要求:

GET http://localhost:49915/api/city
POST http://localhost:49915/api/city

响应:

status: 200 OK
-------------------
{
"cityName": "Gotham"
}

现在我的问题是,

  1. 因为它是一个GET,它应该接受一个POST吗?
  2. 它不应该返回 405 状态代码吗?为什么不呢? (至少我在期待)
  3. 在这种情况下,如果我必须返回 405 怎么办?

最佳答案

As it is a GET, should it supposed to accept a POST?

虽然您因为操作名称和基于约定的路由而假设它是一个 get,但您会误以为 Controller 已被修饰为属性路由。

[Route("api/[controller]")]

因此如果匹配则忽略基于约定的路由。请注意 PUTDELETE

PUT http://localhost:49915/api/city
DELETE http://localhost:49915/api/city

也应该对相同的 Action 起作用。

Shouldn't it return a 405 status code and Why it does not? (at least I'm expecting)

由于没有为操作给出指令,因此该操作在设计上与两个调用匹配。

[Route("api/[controller]")]
[ApiController]
public class CityController : ControllerBase {
// GET api/city?id=2 //Note id would be optional as a query variable.
[HttpGet]
public ActionResult Get(int id) {
try {
var city = new { CityName = "Gotham" };
return Ok(city);
} catch(Exception ex) {
return StatusCode(500);
}
}
}

现在有了HttpGet,如果

POST http://localhost:49915/api/city

或者另一个 HTTP 方法完成后,您将收到 405 错误,因为路径匹配但方法不匹配。

In such case, if I have to return 405 what to be done?

属性路由到位后,框架会为您完成,因此您无需再做任何事。

引用 Routing to controller actions in ASP.NET Core

Mixed routing: Attribute routing vs conventional routing

What distinguishes the two types of routing systems is the process applied after a URL matches a route template. In conventional routing, the route values from the match are used to choose the action and controller from a lookup table of all conventional routed actions. In attribute routing, each template is already associated with an action, and no further lookup is needed.

关于c# - 为什么 http get 方法在 asp.net web api 中接受 http post 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55677496/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com