属性路由ASP Core 3.0

问题描述 投票:0回答:1

如何使用属性路由指定到以下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或...

asp.net-core attributerouting
1个回答
0
投票

我会重新设计您的API控制器。如果您为操作指定一些有意义的名称而不是Get(),那会很好。

© www.soinside.com 2019 - 2024. All rights reserved.