1

アクションにルーティングすることは可能ですか?

    [HttpGet]
    public List<Product> GET(int CategoryId, string option, params string[] properties)
    {
        List<Product> result = new List<Product>();
        result = BusinessRules.getProductsByCategoryId(CategoryId);
        return result;
    }

URL は「/api/Products/CategoryId/full/Name/ProductID/」のようになります

おそらくプロパティがオプションであるため、アクションを呼び出しますが、プロパティパラメータは常にnullです。リクエストの本文で Name および ProductID 引数を渡そうとしましたが、プロパティは null のままです。アクションに 0..N の引数を渡したいので、「params」を使用します。

ルート テンプレートは次のとおりです。

    config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{CategoryId}/{option}/{*properties}",
            constraints: new { CategoryId = @"\d+" },
            defaults: new { option = RouteParameter.Optional, properties = RouteParameter.Optional }
    );
4

1 に答える 1

2

この投稿をチェックしてください: http://www.tugberkugurlu.com/archive/asp-net-web-api-catch-all-route-parameter-binding

カスタム パラメーター バインディングを作成して、キャッチオール クエリ パラメーターを配列に変換します。グローバルに登録するのではなく、必要な場所を装飾するために使用するというアイデアが気に入っています。

 public HttpResponseMessage Get([BindCatchAllRoute('/')]string[] tags) { ...

もちろん、通常のクエリ文字列をいつでも使用できます。それは確かに簡単です:

[HttpGet]
public List<Product> GET(int CategoryId, string option, [FromUri] string[] properties = null)
{
    List<Product> result = new List<Product>();
    result = BusinessRules.getProductsByCategoryId(CategoryId);
    return result;
}

次のように呼び出します: /api/Products/123/full/?properties=Name&properties=ProductID

于 2013-04-10T00:41:01.017 に答える