[英]ASP.NET Web API 2 - Filter POST requests by Query String
I have 2 actions for POST requests on my Web-Api Controller: 对于Web-Api控制器上的POST请求,我有2个操作:
[HttpPost]
[Route("api/messages/{retire=retire}")]
public HttpResponseMessage Update(string retire, [FromBody] message m)
{}
And 和
[HttpPost]
[Route("api/messages")]
public async Task<HttpResponseMessage> Create()
{}
When I do a POST request to the address /api/messages?retire=true
the action executed is the second one, instead the first one that is the one I need to execute. 当我对
/api/messages?retire=true
地址发出POST请求时,执行的动作是第二个动作,而不是第一个动作,即我需要执行的动作。
How can I make requests with query string execute the first action, and request with no query string execute the second one?
如何使带有查询字符串的请求执行第一个操作,而没有查询字符串的请求执行第二个操作?
If I delete the api/messages
action, the mentioned requests work fine. 如果我删除
api/messages
操作,则上述请求可以正常工作。 I thought about joining both together and filter the requests with an if
, but I don't exactly know how. 我考虑过将两者连接在一起,并使用
if
过滤请求,但我不知道怎么做。
Thanks in advance. 提前致谢。
You can just do like this 你可以这样
[HttpPost]
[Route("api/messages")]
public HttpResponseMessage Update(bool retire, [FromBody] message m)
{
if(retire)
{
// write the logic of your first endpoint
}
else
{
// write logic for second
}
}
Finally solved with both actions separated with this syntax: 最后用以下语法分隔的两个动作解决了:
For POST /api/messages?retire=true
requests: 对于
POST /api/messages?retire=true
请求:
[HttpPost]
[Route("api/messages")]
public async Task<HttpResponseMessage> Update(bool retire, [FromBody] message m)
{}
For POST /api/messages
requests: 对于
POST /api/messages
请求:
[HttpPost]
[Route("api/messages")]
public async Task<HttpResponseMessage> Create()
{
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.