[英]How to override ASP.NETCORE route?
how can i use parameter routing?如何使用参数路由?
my project use.Net 6.0我的项目使用.Net 6.0
like:喜欢:
https://localhost:7203/v1/users/123/items?action=delete
and和https://localhost:7203/v1/users/123/items?action=claim
i want use code我想使用代码
[HttpPost]
[Route("/v1/users/{userId}/items")]
public void Delete(string userId)
{
//TODO...
}
[HttpPost]
[Route("/v1/users/{userId}/items")]
public void Claim(string userId)
{
//TODO...
}
[HttpPost("/v1/users/{userId}/items")]
public void PerformUserItemAction([FromRoute] string userId, [FromQuery] string action)
{
if (action == "delete")
{
//...perform delete
}
else if (action == "claim")
{
//...perform claim action
}
}
If you really need use POST
to execute delete action如果您确实需要使用POST
执行删除操作
then your parameter name MUST the same as action's parameter那么你的参数名称必须与动作的参数相同
example:例子:
if your uri is https://localhost:7203/v1/users/123/items?action=delete
如果你的 uri 是https://localhost:7203/v1/users/123/items?action=delete
[HttpPost]
[Route("/v1/users/{userId}/items")]
public void Delete([FromRoute]string userId, string action)
{
// Use your action string to decide action
}
BUT it is better to specify httpDelete to your delete action但是最好为您的删除操作指定 httpDelete
Like HsuTingHuan said.就像许廷欢说的。 It is probably better to use the correct HttpRequestMethods like [HttpDelete]
for the delete action.使用正确的 HttpRequestMethods (如[HttpDelete]
进行删除操作可能会更好。 But if you really need to do it like you said try this:但如果你真的需要像你说的那样做,试试这个:
[Route("/v1/users/{userId}/items?action=claim")]
[Route("/v1/users/{userId}/items?action=delete")]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.