来自 Microsoft 的Web API CRUD 教程:
最后,添加一个按类别查找产品的方法:
public IEnumerable<Product> GetProductsByCategory(string category)
{
return repository.GetAll().Where(
p => string.Equals(p.Category, category, StringComparison.OrdinalIgnoreCase));
}
如果请求 URI 有查询字符串,Web API 会尝试将查询参数与控制器方法上的参数相匹配。因此,“api/products?category=category”形式的 URI 将映射到此方法。
有没有办法让这个通用?如 GetProductsByWhateverIsInTheURI(string WhatIsInTheURI) 或“api/products?whatever=whatever”?
谢谢你。