1

次のシナリオがあります: 次のような ODataQueryOptions を受け入れる "GetAll" メソッドを持つ ProductsController:

    [GET("Products", RouteName = "GetAllProducts")]
    public ProductDTO[] Get(ODataQueryOptions options)
    {  
       //parse the options and do whatever...
        return new ProductDTO[] { };
    }

次のような GetProducts メソッドを持つ CategoryController:

    [GET("Category/{id}/Products", RouteName = "GetProductsByCategory")]
    public HttpResponseMessage GetProducts(int id, ODataQueryOptions options)
    {
        //Request URL can be "api/Category/12/Products?$select=Name,Price&$top=10"
        //Need to do a redirect the ProductsController "GetAllProducts" action
        HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.RedirectMethod);
        msg.Headers.Location = new Uri(Url.Link("GetAllProducts",options));
        // how do we send the odata query string"$select=Name,Price&$top=10"
        //to the ProductsController? passing "options" directly does not work!
        return msg;
    }

CategoryController の特定のカテゴリで製品をフェッチするロジックを再定義したくありません。する方法はありますか

1) リダイレクトの一部として ODataQueryOptions を渡しますか?

2) オプションを変更して、フィルター条件を追加できますか? 上記の例では、「GetAllProducts」が次のリクエストを受け取るように、リダイレクトを行う前に現在の CategoryID に追加のフィルター条件を追加したいと思います:「api/Products?$select=Name,Price&$top=10& $ 」フィルター=CategoryID eq 12 "

上記は理にかなっていますか、それとも別の方法でアプローチする必要がありますか?

前もって感謝します。

4

1 に答える 1

3

このヘルパーを使用して、リクエストから OData クエリ文字列を取得できます。

    private static string GetODataQueryString(HttpRequestMessage request)
    {
        return
            String.Join("&", request
                                .GetQueryNameValuePairs()
                                .Where(kvp => kvp.Key.StartsWith("$"))
                                .Select(kvp => String.Format("{0}={1}", kvp.Key, Uri.EscapeDataString(kvp.Value))));
    }
于 2013-03-18T20:51:28.753 に答える