[英]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.