在一个对象中重新组合WebAPI HttpGet方法参数
本文关键字:WebAPI HttpGet 方法 参数 组合 一个对象 新组合 | 更新日期: 2023-09-27 18:00:33
我的WebAPI控制器中有几个HttpGet方法,它们中的大多数都接受相同的可选参数列表。我想知道是否有一种方法可以将它们重新组合为一个自定义类对象?
示例:
更换
[HttpGet]
public Object RunReport(int id, int? page = 1, int? pageSize = 50, String sortedBy = null, String sortDir = null, String eez = null, String species = null, int? groupBy = null, int? year = null, String flag = null, int? minYear = null, int? maxYear = null, String obsvPrg = null, int? setType = null, String setTypeCat = null, int? tripId = null){}
带
[HttpGet]
public Object RunReport(int id, int? page = 1, int? pageSize = 50, QueryParams queryParams=null){}
使用以下类别的
public class QueryParams
{
public string SortedBy { get; set; }
public string SortDir { get; set; }
public string Eez { get; set; }
public string Species { get; set; }
public int? GroupBy { get; set; }
public int? Year { get; set; }
public string Flag { get; set; }
public int? MinYear { get; set; }
public int? MaxYear { get; set; }
public string ObsvPrg { get; set; }
public int? SetType { get; set; }
public string SetTypeCat { get; set; }
public int? TripId { get; set; }
public string Gear { get; set; }
public bool IsCount { get; set; }
}
感谢您的帮助
取决于你想如何传入该对象,你可以选择
public object RunReport([FromUrl] QueryParams params)
{
...
}
使用Json/XML负载,然后标记您的方法:
public object RunReport([FromBody] QueryParams params)
{
...
}
或者(也适用于表单数据+查询数据)
public object RunReport([ModelBinder] QueryParameter params)
{
...
}