简体   繁体   English

具有许多参数的ASP.Net Core Web API Controller操作

[英]ASP.Net Core Web API Controller action with many parameters

I have ASP.Net Core Web API Controller's method that returns List<Car> based on query parameters. 我有ASP.Net Core Web API Controller的方法,该方法基于查询参数返回List<Car>

[HttpPost("cars")]
public async Task<List<Car>> GetCars([FromBody] CarParams par)
{
    //...
}

Parameters are grouped in record type CarParams . 参数按记录类型CarParams分组。 There are about 20 parameters: 大约有20个参数:

public class CarParams
{
    public string EngineType { get; set; }
    public int WheelsCount { get; set; }
    /// ... 20 params
    public bool IsTruck { get; set; }
}

I need to change this method from POST to GET, because I want to use a server-side caching for such requests. 我需要将此方法从POST更改为GET,因为我想对此类请求使用服务器端缓存。

Should I create a controller's method with 20 params? 是否应该使用20个参数创建控制器的方法?

[HttpGet("cars")]
public async Task<List<Car>> GetCars(string engineType, 
                                      int wheelsCount, 
                                  /*...20 params?...*/
                                         bool isTruck)
{
    //...
}

If this is the case: Is there a way to automatically generate such a complex URL for a client-side app? 如果是这样的话:有没有一种方法可以自动为客户端应用生成如此复杂的URL?

You can keep the model. 您可以保留模型。 Update the action's model binder so that it knows where to look for the model data. 更新操作的模型绑定程序,以便它知道在哪里查找模型数据。

Use [FromQuery] to specify the exact binding source you want to apply. 使用[FromQuery]指定要应用的确切绑定源。

[HttpGet("cars")]
[Produces(typeof(List<Car>))]
public async Task<IActionResult> GetCars([FromQuery] CarParams parameters) {

    //...

    return Ok(data);
}

Reference Model Binding in ASP.NET Core ASP.NET Core中的参考模型绑定

只需使用[FromUrl]更改[FromBody]属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 混合web api动作和非api动作相同的控制器asp.net核心 - Mix web api action and non api action same controller asp.net core 在ASP.NET Core Web API中使用依赖项注入时未调用Controller Action方法 - Controller Action method not called while using dependency injection in asp.net core web api 为什么在 Asp.net 核心 web Z8A5DA52ED1206747D359EA70 - Why use async/await in an action in controller in Asp.net core web api Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller - Call MySql stored procedure which take 2 parameters from asp.net core 2.2 web API controller 如何更改 ASP.NET Core API 中的默认控制器和操作? - How to change the default controller and action in ASP.NET Core API? ASP.NET Web API后期操作参数 - Asp.net web api post action parameters ASP.NET Web API操作方法参数的依赖注入 - Dependency injection for ASP.NET Web API action method parameters 动作过滤器在 asp.net 核心 Web API 中不起作用 - Action Filter is not working in asp.net core Web API asp.net 核心 3 Web Api ! 将 json 发送到 POST 操作 - asp.net core 3 Web Api ! send json to POST Action ASP.NET Web API请求未达到控制器操作 - ASP.NET Web API request does not reach controller action
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM