AxD*_*AxD 10 asp.net asp.net-web-api-routing asp.net-core
我记得在 ASP.NET Web API 中,用 HTTP 命令(例如GetList()
=> HTTP GET
、Delete()
=> HTTP DELETE
)作为Web API REST 方法名称的前缀就足以正确路由传入调用。
我还记得在ASP.NET的Web API参数匹配发生这样即使Get(int id)
和Get(int id, string name)
获得自动转到相应的部门,而不需要任何属性。
public class MyController
{
public ActionResult Get(int id) => ...
public ActionResult Get(int id, string name) => ...
public ActionResult DeleteItem(int id) => ...
}
Run Code Online (Sandbox Code Playgroud)
这不是在 ASP.NET Web API Core 中都可用的吗?
Ton*_*ams 24
您只需要将 Route 添加到控制器的顶部。
使用 api、控制器和操作指定路由:
[Route("api/[controller]/[action]")]
[ApiController]
public class AvailableRoomsController : ControllerBase
{
...
}
Run Code Online (Sandbox Code Playgroud)
我们既不能进行操作重载,也不能将操作名称作为 Http 动词前缀。路由在 ASP.NET Core 中的工作方式与在 ASP.NET Web Api 中的工作方式不同。
但是,您可以简单地组合这些操作,然后在内部分支,因为如果您作为查询字符串发送,所有参数都是可选的
[HttpGet]
public ActionResult<string> Get(int id, string name)
{
if(name == null){..}
else{...}
}
Run Code Online (Sandbox Code Playgroud)
或者,如果您发送路由数据,则需要使用属性路由来指定每个 api:
[HttpGet("{id}")]
public ActionResult<string> Get(int id)
{
return "value";
}
[HttpGet("{id}/{name}")]
public ActionResult<string> Get(int id, string name)
{
return name;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12676 次 |
最近记录: |