【问题标题】:Attribute Routing ASP Core 3.0属性路由 ASP Core 3.0
【发布时间】:2019-12-14 22:22:48
【问题描述】:

如何使用属性路由指定到以下 API 端点的路由?

返回的产品对象具有一些属性以及分别包含 null 或 Store 和 Manufacturer 对象的 Store 和 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 返回带有 Store 填充且 Manufacturer = null 的 Product 对象。这很好。
    • /api/Products/true/1 Store 1 上的过滤器也不错。
    • 但这是一个愚蠢的 url(真的吗?- 产品、商店还是制造商?)
  • [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 参数。

我可以继续描述我的其他实验,但我只想说,没有一个给我我正在寻找的结果。我想我误解了属性路由,但也许它不是为了做我想做的事情而设计的。

我想我希望看到的是使用查询字符串修改单个 url,例如,

  • /api/Products?stores=true&amp;manufacturerId=1获取由制造商 1 生产的带有商店信息的产品,或
  • /api/Products?storeId=1,stores=true,manufacturers=true 获取有关商店 1 产品的完整详细信息

或者,也可以有

  • /api/Products/Stores/1 获取商店 1 中产品的包含商店信息的产品
  • /api/Products/Stores/Manufacturers/1 获取由制造商 1 生产的带有商店和制造商信息的项目

实际上,我对任何可读的 URL 架构都持开放态度。

谢谢!

【问题讨论】:

  • “没有给我我正在寻找的结果” – 你在寻找什么
  • 更新了问题

标签: asp.net-core attributerouting


【解决方案1】:

我会稍微重新设计您的 API 控制器。如果你给你的行为一些有意义的名字而不是Get(),那会很好。

我还将创建一个服务,该服务具有提供所请求产品的逻辑并替换 _ctx

    // Default - returns all Products 
    [HttpGet ("")]
    public async Task<ActionResult<IEnumerable<Product>>> GetAllProducts()
    {
        var products = await _ctx.GetAllProducts();
        return products.ToList();
    }

    // stores = true will set Product.Store = to the store object
    // storeId = 1 will filter Products to those in Store 1
    [HttpGet ("Stores/{storeId?}")]
    public async Task<ActionResult<IEnumerable<Product>>> GetProductsByStore(int? storeId)
    {
        if (storeId.HasValue)
        {
            // get products by store id
            products = await _ctx.GetProductsByStoreId(storeId);
        }
        else
        {
             products = await _ctx.GetProductsByCurrentStore(); // just a suggestion I 
         have no idea what you mean by saying "..set Product.Store = to the store object"
        }

        return products.ToList();
    }

    // manufacturer = true will st Product.Manufacturer to a manufacturer object
    // manufacturerId = 1 will filter Product to those produced by manufacturer 1
    [HttpGet ("Manufacturers/{manufacturerId?}")]
    public async Task<ActionResult<IEnumerable<Product>>> GetProductsByManufacturer(int? manufacturerId)
    {
        if (manufacturerId.HasValue)
        {
            products = await _ctx.GetProductsByManufacturerId(manufacturerId);
        }
        else
        {
            products = await _ctx.GetProductsByManufacturer();
        }

        return products.ToList();
    }

【讨论】:

  • 谢谢。我想过打破控制器方法,但它是有限的。例如,我可能想要过滤 storeId 但仍将制造商数据作为对象的一部分返回。属性路由是否意味着控制器方法必须简单?
  • @bob 我看到你正在返回Product,如果Manufacturer 是该对象的一部分,我在按商店过滤时看不到恢复它的问题。我不确定我明白你在问什么,路由与你如何构建控制器逻辑无关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-19
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-10-13
  • 2020-03-18
相关资源
最近更新 更多