如何使用属性路由指定到以下API端点的路由?
返回的产品对象具有一些属性以及一个Store和Manufacturer字段,分别包含null或一个Store and Manufacturer对象。
[Route("api/[controller]")]
[ApiController]
public class ProductsController : ControllerBase {
[HttpGet ("")]
public async Task<ActionResult<IEnumerable<Product>>> Get(
bool stores = false, int storeId = 0,
bool manufacturers = false, int manufacturerId = 0
) {
// Default - returns all Products
// stores = true will set Product.Store = to the store object
// storeId = 1 will filter Products to those in Store 1
// manufacturer = true will st Product.Manufacturer to a manufacturer object
// manufacturerId = 1 will filter Product to those produced by manufacturer 1
var products = await _ctx.GetProducts(
stores, storeId,
manufacturers, manufacturerId
);
return products.ToList();
}
我希望有一些可读的路径来访问和适当地设置参数。
[HttpGet ("")]
/api/Products
以返回Store和Manufacturer = null的对象。这是可取的。("")
单独使用时不需要,但如果添加其他路线,则是必需的。[Route("{stores=false}/{storeId=0})]
/api/Products/true
返回带有商店已填充且制造商= null的产品对象。很好/api/Products/true/1
在商店1上的过滤器也不错。 [Route("Stores/{storeId?})]
/api/Products/Stores/1
使stores = false,尽管过滤了Store 1,但不包括stores对象。路径数据中没有“商店”。它在Request.Path中,但我不想去那里。/api/Products/Stores?storeId=1
不起作用。 Request.QueryString.Value =?storeId = 1,但不会绑定到我的storeId参数。我可以继续描述我的其他实验,但可以说,没有一个给我想要的结果。我认为我对属性路由有误解,但也许并不是为了执行我想做的事情。
我想我想看到的是用查询字符串(例如,)修改单个网址,>
/api/Products?stores=true&manufacturerId=1
以获取具有制造商1生产的商店信息的产品,或/api/Products?storeId=1,stores=true,manufacturers=true
获取有关商店1中产品的完整详细信息或者,也可以有
/api/Products/Stores/1
以获取具有商店1中产品的商店信息的产品]/api/Products/Stores/Manufacturers/1
以获取包含制造商1生产的项目的商店和制造商信息的项目实际上,我对任何可读的URL模式都持开放态度。
谢谢!
如何使用属性路由指定到以下API端点的路由?返回的产品对象具有一些属性以及包含null或...
我会重新设计您的API控制器。如果您为操作指定一些有意义的名称而不是Get()
,那会很好。