WebAPI.NET-按参数筛选POST请求
我使用POST创建消息并更新它们(我需要使用POST,而不是PUT)。API有以下说明:WebAPI.NET-按参数筛选POST请求,.net,rest,webapi2,.net,Rest,Webapi2,我使用POST创建消息并更新它们(我需要使用POST,而不是PUT)。API有以下说明: POST/api/message POST/api/message?更新消息 我如何区分两者?我想我必须在函数中执行if: [HttpPost] [Route("api/message")] public async Task<HttpResponseMessage> Handle() {} [HttpPost] [路由(“api/消息”)] 公共异步任务句柄() {} 检查请求是否包含参数
POST/api/message
POST/api/message?更新消息
我如何区分两者?我想我必须在函数中执行if
:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Handle()
{}
[HttpPost]
[路由(“api/消息”)]
公共异步任务句柄()
{}
检查请求是否包含参数更新消息
有没有办法解决这个问题?谢谢。查询字符串参数有一个键和一个值。您应该向“update_message”参数添加一个值,并使用它来决定是创建还是更新消息。 在route属性中,可以定义查询字符串param
[HttpPost, Route("api/message/{update_message=update_message}")]
public async Task<HttpResponseMessage> Handle(string update_message)
{
if(string.Equals("true", update_message)
{
// update
}
else
{
//create
}
}
[HttpPost,路由(“api/message/{update\u message=update\u message}”)]
公共异步任务句柄(字符串更新消息)
{
if(string.Equals(“true”,更新消息)
{
//更新
}
其他的
{
//创造
}
}
查询字符串参数有一个键和一个值。您应该在“update_message”参数中添加一个值,并使用它来决定是创建还是更新消息。
在route属性中,可以定义查询字符串param
[HttpPost, Route("api/message/{update_message=update_message}")]
public async Task<HttpResponseMessage> Handle(string update_message)
{
if(string.Equals("true", update_message)
{
// update
}
else
{
//create
}
}
[HttpPost,路由(“api/message/{update\u message=update\u message}”)]
公共异步任务句柄(字符串更新消息)
{
if(string.Equals(“true”,更新消息)
{
//更新
}
其他的
{
//创造
}
}
最终解决了这两个操作,并用以下语法分隔:
对于POST/api/message?update_message=true
请求:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Update(bool update_message, [FromBody] message m)
{}
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Create()
{}
最后,使用以下语法将两个操作分开解决: 对于
POST/api/message?update_message=true
请求:
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Update(bool update_message, [FromBody] message m)
{}
[HttpPost]
[Route("api/message")]
public async Task<HttpResponseMessage> Create()
{}