我有一个非常“GET”密集的ASP.NET WEB API应用程序。 大多数方法接受方法签名变化的params的变化
例如
[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; }
}
如果我想保留我的方法变体并仍然使用数据注释有一种方法我可以使用它们而不创建所有输入模型(是否有办法以某种方式“内联”使用它们?)?