我有一个非常“GET”密集型的 ASP.NET WEB API 应用程序。大多数方法接受方法签名不同的参数变化
例如
[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year)
{
//...
}
[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category)
{
//...
}
[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category, int minPrice)
{
//...
}
[HttpGet]
public async Task<HttpResponseMessage> GetProducts(int year, string category, decimal minPrice, decimal maxPrice)
{
//...
}
我想使用数据注释,System.ComponentModel.DataAnnotations
但我注意到我必须将所有简单的 url 参数转换为模型?换句话说,我必须为每个方法变体创建输入模型(参见下面的示例)。
[HttpGet]
public async Task<HttpResponseMessage> GetProducts([FromUri] yearModel)
{
//...
}
public class YearModel
{
[Required]
[Range(1966, 2013)]
public int Year { get; set; }
}
如果我想保留我的方法变体并仍然使用数据注释,有没有一种方法可以在不创建所有输入模型的情况下使用它们(有没有办法以某种方式“内联”使用它们?)?