[英]Update the POST endpoint with additional input without hampering existing functionality in .Net Web API
Consider the scenario where I have a POST endpoint which takes an ID
in route parameter and a specific request payload Range
.考虑我有一个 POST 端点的场景,该端点在路由参数和特定请求有效负载
Range
中采用ID
。 I want to update this endpoint to also include an additional parameter Type
by providing it into POST request payload.我想通过将其提供到 POST 请求有效负载中来更新此端点以包含附加参数
Type
。
I have considered multiple options here:我在这里考虑了多种选择:
[Route("updateItem/{id:int}/{type}")]
. [Route("updateItem/{id:int}/{type}")]
。Range
on server and update the existing consumers.Range
并更新现有消费者。[FromBody]
in the controller arguments.[FromBody]
。 However these approaches will hamper the existing consumers of the API.然而,这些方法将阻碍 API 的现有消费者。
[HttpPost]
[Route("updateItem/{id:int}")]
public IHttpActionResult UpdateItem(int id, Range range)
{
return Ok(Service.UpdateItem(id, range));
}
Is there a way to pass on the information of Type
to the API without impacting existing consumers of the API?有没有办法将
Type
信息传递给 API 而不会影响 API 的现有消费者?
Try this尝试这个
[HttpPost]
[Route("updateItem/{id:int}"/{type:Type?})]
public IHttpActionResult UpdateItem(int id, Type? type=null, Range range)
{
if (type != null)
{
return Ok(Service.UpdateItem(id, range));
}
else
{
//implementation for type parameter
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.