简体   繁体   English

在不妨碍.Net Web API 中的现有功能的情况下,使用额外的输入更新 POST 端点

[英]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:我在这里考虑了多种选择:

  • Updating the route parameter to provided additional parameter in route.将路由参数更新为路由中提供的附加参数。 [Route("updateItem/{id:int}/{type}")] . [Route("updateItem/{id:int}/{type}")]
  • Changing the request payload DTO Range on server and update the existing consumers.更改服务器上的请求负载 DTO Range并更新现有消费者。
  • Using [FromBody] in the controller arguments.在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM