WebAPI .NET - 按参数过滤 POST 请求 [英] WebAPI .NET - POST request filter by parameters
本文介绍了WebAPI .NET - 按参数过滤 POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 POST 创建消息并更新它们(我需要使用 POST,而不是 PUT).该 API 具有以下说明:
I use POST to create messages and to update them (I need to use POST, not PUT). The API has the following instructions:
POST/api/message
POST/api/message?update_message
我如何区分两者?猜猜我必须在函数中做一个 if
:
How can I difference between both? Guess I have to do an if
in the function:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Handle()
{}
检查请求是否包含参数update_message.
checking if the request contains the parameter update_message.
知道如何解决这个问题吗?谢谢.
Any idea on how to solve that? Thanks.
推荐答案
最后用这个语法把两个动作分开来解决:
Finally solved with both actions separated with this syntax:
对于 POST/api/message?update_message=true
请求:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Update(bool update_message, [FromBody] message m)
{}
对于 POST/api/message
请求:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Create()
{}
这篇关于WebAPI .NET - 按参数过滤 POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文