【问题标题】:.Net Core Web API to return different data models to different clients.Net Core Web API 将不同的数据模型返回给不同的客户端
【发布时间】:2020-07-13 18:19:36
【问题描述】:

我在 .Net 核心中开发了 Web API。我想为不同的客户端从同一个 Action 方法返回不同的数据模型。

【问题讨论】:

  • 为什么?听起来通过路由或版本控制可以更好地解决问题,但您能告诉我们为什么需要这个吗?
  • 这是一种要求,不同的字段属性将暴露给不同的客户端。例如客户端 1:{ prop1:value1, prop2: value2}。客户端 2:{ prop1:value 1, prop3: value3}
  • 巨大差异?控制器中更好的独立端点。如果 small 可以只使用一个模型,并在序列化程序中设置为忽略 null 属性(它们不会写入 json)并且不填充它们。

标签: c# .net-core asp.net-mvc-5 webapi asp.net-mvc-5.1


【解决方案1】:

您可以根据不同的选项更改操作的结果,但是客户会很奇怪,而且我从来没有看到有人或项目会这样做,这会使调试更加困难。 当一个服务工作时,它总是应该暴露预期的行为,我们应该知道它什么时候成功它给我们一个人对象,当它失败时,它返回一个失败消息,改变客户端的框架是最糟糕的情况。 更好的方法是使用不同的 API,当客户端需要不同的结果时,我们必须公开不同的 API,这些单独的 API 应该遵守上述规则。

【讨论】:

    【解决方案2】:

    您可以通过将返回类型声明为Task<IActionResult>,从一个端点返回您想要的任何模型。

    假设您有一个CustomersController,那么GET 端点将是api/customers?clientType=client1。现在您希望基于clientType 参数的不同客户的不同信息。

    namespace walletapi.Controllers
    {
        [ApiController]
        [Authorize]
        public class CustomersController : ControllerBase
        {
            public async Task<IActionResult> Get(string clientType)
            {
                if(clientType=="type1"){
                   var type1Response = new CustomerInfoForClient1() {Property1="value1"};
                   return Ok(type1Response );
                }
                if(clientType=="type2"){
                   var type2Response = new CustomerInfoForClient2() {Property1="value2"};
                   return Ok(type2Response);
                }
              return NotFound("client type is not found");
    
            }
        }
        public class CustomerInfoForClient1
        {
          public string Property1{get;set;}
        }
    
       public class CustomerInfoForClient2
       {
         public string Property3{get;set;}
       }
    }
    

    【讨论】:

      【解决方案3】:

      如果您不开发微服务,通常在一个端点中拥有多个结果集并不是一个好主意。但是,如果您需要,可以使用 IActionResult Type 。使用这种类型,您不必声明固定的返回类型。你可以这样使用。

          [HttpGet("list/{clientType}")]
          [ProducesResponseType(StatusCodes.Status200OK)]
          [ProducesResponseType(StatusCodes.Status404NotFound)]
          public IActionResult ReturnSomeList(int clientType)
          {
              var firstList = new List<string>();
              for (int i = 0; i < 3; i++)
              {
                  firstList.Add($"str {i}");
              }
      
              var secondList = new List<int>();
              for (int i = 0; i < 5; i++)
              {
                  secondList.Add(i);
              }
      
              if (clientType == 0)
              {
                  return Ok(firstList);
              }
      
              if (clientType == 1)
              {
                  return Ok(secondList);
              }
      
              return NotFound("Can not find something");
          }
      

      【讨论】:

      • 我想根据客户端返回响应,即使有一些异常。示例:客户端 1:{ param1:value1,错误:List(),param 3:value3}。客户端 2: { param1:value1, 错误: List(), param 4: value4}
      • @RabiaBasri 我不确定你的意思。但是看看我更新的例子,你可以根据你的情况返回不同类型的列表。您可以根据需要修改我的示例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-02
      • 2017-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多