ODATA 使用 C# ASP.NET 4.0 的服务操作

发布于 2024-09-26 11:11:08 字数 874 浏览 10 评论 0原文

我通过 C# ASP.NET 应用程序连接到 ODATA 服务,该应用程序具有以下服务操作:

GetItems(int? itemID, double? price)

我可以在浏览器中毫无问题地使用它,例如,

http://api.mycompany.com/companycatalogue/GetItems?itemID=4

我了解如何使用 LINQ to Entities 来使用 ODATA 服务,但不能找不到关于如何像上面在 C# 中那样使用服务操作的合理解释。我已在我的 Visual Studio 解决方案中对该服务进行了 Web 引用。

到目前为止,我对数据的日常使用有类似的情况:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
    var result = from i in dataContext.Items select i;  //just an example

    //this is where I get into problems
    var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}

有什么指针吗?

I am connecting to an ODATA Service via a C# ASP.NET application, which has service operations such as:

GetItems(int? itemID, double? price)

I can consume this without issues in my browser, e.g.

http://api.mycompany.com/companycatalogue/GetItems?itemID=4

I understand how to use LINQ to Entities to consume an ODATA service, but can't find a decent explanation of how to consume service operations like the one above in C#. I have made a web reference to the service in my Visual Studio solution.

So far, I have something like this for my usual consuming of the data:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities (new Uri("http://api.mycompany.com/companycatalogue/"));
    var result = from i in dataContext.Items select i;  //just an example

    //this is where I get into problems
    var operationResults = CompanyCatalogue.GetItems(6, 20.5); //I just made this up
}

Any pointers?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-10-03 11:11:08

好的,得到答案了:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities(); 

    DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
        .AddQueryOption("paramName", 6)
        .AddQueryOption("paramName2", 20.5);

    List<GetItemsResult> items = q.Execute().ToList();
}

OK, got the answer:

using CompanyCatalogue; //my web reference
...
protected void Page_Load(object sender, EventArgs e)
{
    CompanyCatalogueEntities dataContext = new CompanyCatalogueEntities(); 

    DataServiceQuery<GetItemsResult> q = dataContext.CreateQuery<GetItemsResult>("GetItems")
        .AddQueryOption("paramName", 6)
        .AddQueryOption("paramName2", 20.5);

    List<GetItemsResult> items = q.Execute().ToList();
}
暖阳 2024-10-03 11:11:08

这可能对你有帮助。
此示例代码用于使用 WFC 数据服务中的服务操作。该服务操作(GetRowCount)返回整数(int)类型值。输入参数名称是“代码”

    var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
            int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();

This may be help for you.
This sample code used to consume the service operation in the WFC Data Service. This service operation(GetRowCount) returns the integer(int) type value. input para name is "code"

    var q = context.CreateQuery<int>("GetRowCount").AddQueryOption("code", "'" + serviceProvider.Code + "'");
            int RecordCount = context.Execute<int>(new Uri(q.RequestUri.ToString().Replace("GetRowCount()", "GetRowCount"))).FirstOrDefault();
奈何桥上唱咆哮 2024-10-03 11:11:08

您尝试过使用 HttpWebRequest 吗?

Have you tried using HttpWebRequest?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文