簡體   English   中英

屬性路由 ASP Core 3.0

[英]Attribute Routing ASP Core 3.0

如何使用屬性路由指定到以下 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 對象。 這很好。
    • 商店 1 上的/api/Products/true/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獲取包含商店/api/Products/Stores/1中產品的商店信息的產品
  • /api/Products/Stores/Manufacturers/1獲取包含制造商 1 生產的項目的商店和制造商信息的項目

實際上,我對任何可讀的 URL 模式都持開放態度。

謝謝!

我會稍微重新設計一下你的 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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM