【问题标题】:Best way to pass throug URL query for oData Service .net c#通过 oData Service .net c# 的 URL 查询的最佳方式
【发布时间】:2015-01-16 14:45:00
【问题描述】:

我有以下几点:

  1. 支持 WebAPI oData 的端点应用程序,背后有一个小型数据库
  2. .NET MVC 前端,带有一些 HTML5 内容和一些程序逻辑

我通过 (1) 中的服务和 (2) 中的应用程序使用数据。

由于应用程序 (1) 启用了 oData,我想为高级用户提供在应用程序 (2) 中时在浏览器中编写 URL 过滤器的选项。所以他们可以附加这样的东西 $top=2&$orderby=name

获取该 url 过滤器并传递到应用程序 (2) 中的 oData 服务的最佳方法是什么?我找不到简单传递字符串的方法。我发现使用 lync 查询的唯一方法是 AddQueryOptions。

有什么建议吗?

  // TODO: Replace with your local URI.
        string serviceUri = "http://localhost:14800/";
        var container = new Default.Container(new Uri(serviceUri));

        var product = new ODataDemoService.Models.Product();

        // grab 
        Uri myUri = new Uri("http://localhost:14800/Products?$top=2&$orderby=name");


        NameValueCollection nv = HttpUtility.ParseQueryString(myUri.Query);
        foreach (string key in nv)
        {
            // build a filter string here 
        }


        // Append the filter string using AddQueryOptions
        foreach (var p in container.Products.AddQueryOption("$filter","(Name eq 'Hat') or (Name eq 'Socks')"))
        {
            Console.WriteLine("{0} {1} {2}", p.Name, p.Price, p.Category);
        }   

【问题讨论】:

    标签: c# .net odata


    【解决方案1】:

    你可以使用 DataServiceContext.Execute

    var products = container.Execute<Product>(new Uri()), "GET"); // You can pass the Uri as what you want
    foreach(var p in products)
    {
        Console.WriteLine("{0} {1} {2}", p.Name, p.Price, p.Category);
    }   
    

    【讨论】:

      猜你喜欢
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 2023-03-07
      • 1970-01-01
      • 1970-01-01
      • 2018-08-30
      • 2013-08-22
      相关资源
      最近更新 更多