C# 映射自定义Odata函数时出现问题

C# 映射自定义Odata函数时出现问题,c#,entity-framework-4,odata,C#,Entity Framework 4,Odata,我试图在ODataWebService上执行一个自定义函数(在本例中是Proof())。我正试图找出从客户端执行此controllers Proof()方法需要使用的URL。这显然是错误映射的,因为在调试模式下运行并进行客户端调用时,我看不到执行的方法(_http://localhost:53610/Receipt/AccountsReceivableWebService.Proof()). 我应该使用什么URL从客户端执行此方法?更好的是,如何从WebApiConfig正确映射此路径?欢迎评

我试图在ODataWebService上执行一个自定义函数(在本例中是Proof())。我正试图找出从客户端执行此controllers Proof()方法需要使用的URL。这显然是错误映射的,因为在调试模式下运行并进行客户端调用时,我看不到执行的方法(_http://localhost:53610/Receipt/AccountsReceivableWebService.Proof()).

我应该使用什么URL从客户端执行此方法?更好的是,如何从WebApiConfig正确映射此路径?欢迎评论!提前谢谢

webservice控制器如下所示:

using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Routing;
using AccountsReceivableWebService.Models;


namespace AccountsReceivableWebService.Controllers
{
  public class ReceiptController : ODataController
{
ReceiptContext db = new ReceiptContext() { Receipt = new Receipt() { ReceiptLines = new List<ReceiptLine>() } };

private bool ReceiptExitsts(string key)
{
  return true;
}

public IHttpActionResult Get()
{


  return Ok(db.Receipt);
}

public IHttpActionResult Post(Receipt receipt)
{
  if (!ModelState.IsValid) return BadRequest(ModelState);
  if (!receipt.Insert()) BadRequest("Poorly formed receipt.");
  db.Receipt = receipt;
  return Created(db.Receipt);
}

/// <summary>
/// Checks that this receipt is a valid receipt
/// </summary>
/// <returns></returns>

[HttpGet]
public IHttpActionResult Proof()
{
  return Ok();
}


  }
}
使用System.Collections.Generic;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Linq;
Net系统;
使用System.Threading.Tasks;
使用System.Web.Http;
使用System.Web.OData;
使用System.Web.OData.Routing;
使用AccountsReceivebleWebService.Models;
命名空间帐户ReceiveableWebService.Controllers
{
公共类接收方控制器:ODataController
{
ReceiptContext db=new ReceiptContext(){Receipt=new Receipt(){ReceiptLines=new List()}};
私有bool receiptexits(字符串键)
{
返回true;
}
public IHttpActionResult Get()
{
返回Ok(db.收据);
}
公共IHttpActionResult Post(收据)
{
如果(!ModelState.IsValid)返回BadRequest(ModelState);
如果(!receipt.Insert())请求错误(“格式错误的收据”);
db.收据=收据;
已创建退货(db.收据);
}
/// 
///检查此收据是否有效
/// 
/// 
[HttpGet]
公共IHttpActionResult证明()
{
返回Ok();
}
}
}
WebApiConfig.cs是我试图映射我想要执行的函数的地方。代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using AccountsReceivableWebService.Models;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;


namespace AccountsReceivableWebService
{
 public static class WebApiConfig
 {
  public static void Register(HttpConfiguration config)
  {
  // Web API configuration and services

  // Web API routes
  ODataConventionModelBuilder builder = new ODataConventionModelBuilder();
  builder.EntitySet<Receipt>("Receipts");

  builder.Namespace = "AccountsReceivableWebService";
  builder.EntityType<Receipt>().Collection.Function("Proof").Returns<string>();


  config.MapODataServiceRoute(
    routeName: "ODataRoute",
    routePrefix: null,
    model: builder.GetEdmModel());


  }
 }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web.Http;
使用AccountsReceivebleWebService.Models;
使用System.Web.OData.Builder;
使用System.Web.OData.Extensions;
命名空间帐户ReceiveableWebService
{
公共静态类WebApiConfig
{
公共静态无效寄存器(HttpConfiguration配置)
{
//Web API配置和服务
//Web API路由
ODataConventionModelBuilder=新ODataConventionModelBuilder();
建造商实体集(“收据”);
builder.Namespace=“AccountsReceivableWebService”;
builder.EntityType().Collection.Function(“Proof”).Returns();
config.MapODataServiceRoute(
routeName:“ODataRoute”,
routePrefix:null,
模型:builder.GetEdmModel());
}
}
}
编辑:我收到的错误是404.0-未找到,这使我相信这是一个映射问题

元数据:

<edmx:Edmx xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx" Version="4.0">
  <edmx:DataServices>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="AccountsReceivableWebService.Models">
      <EntityType Name="Receipt">
        <Key>
          <PropertyRef Name="ReceiptId"/>
        </Key>
        <Property Name="ReceiptId" Type="Edm.String" Nullable="false"/>
        <Property Name="comments" Type="Edm.String"/>
        <Property Name="remitter" Type="Edm.String"/>
        <Property Name="ReceiptLineId" Type="Edm.String"/>
        <NavigationProperty Name="ReceiptLines" Type="Collection(AccountsReceivableWebService.Models.ReceiptLine)"/>
      </EntityType>
      <EntityType Name="ReceiptLine">
        <Key>
          <PropertyRef Name="ReceiptLineId"/>
        </Key>
        <Property Name="ReceiptLineId" Type="Edm.String" Nullable="false"/>
        <Property Name="Comments" Type="Edm.String"/>
        <Property Name="FiscalYear" Type="Edm.String"/>
        <Property Name="CostCenter" Type="Edm.String"/>
        <Property Name="object" Type="Edm.String"/>
        <Property Name="Amount" Type="Edm.String"/>
        <Property Name="Function" Type="Edm.String"/>
      </EntityType>
    </Schema>
    <Schema xmlns="http://docs.oasis-open.org/odata/ns/edm" Namespace="AccountsReceivableWebService">
      <Function Name="Proof" IsBound="true">
        <Parameter Name="bindingParameter" Type="Collection(AccountsReceivableWebService.Models.Receipt)"/>
        <ReturnType Type="Edm.String" Unicode="false"/>
      </Function>
      <EntityContainer Name="Container">
        <EntitySet Name="Receipts" EntityType="AccountsReceivableWebService.Models.Receipt"/>
      </EntityContainer>
    </Schema>
  </edmx:DataServices>
</edmx:Edmx>

这就是我如何在我的一个项目中使用bounded to collection函数的方法

在WebApi.config中

var function = customerBuilder.EntityType<MyType>().Collection.Function("MyFunction");
function.Returns<MyReturnType>();
function.Parameter<string>("myStringParameterName");
这就是我在MVC控制器中使用数据的原因

[HttpGet]
public IHttpActionResult MyFunction(string myStringParameterName)
{
    //DBContextStuff
    return Ok(new MyReturnType())
}
var myData = MyODataClient.MyType.MyFunction(myStringParameterName).GetValue();

希望能有所帮助。

乍一看,你似乎做的每件事都很正确。当你提出请求时,你看到了什么样的回应?您的$metadata看起来像什么?我的控制器似乎给它提供了错误的路径(source.GetPath(“AccountsReceivableWebService.Proof”+parameterString)),它等同于“Receipts/AccountsReceivableWebService.Proof()”而不是_http://localhost:123456/Receipts/AccountsReceivableWebService.Proof()". 当我输入完整地址时,我的web服务按预期运行。我已更新了用于创建客户端的OData v4客户端代码生成器(在这样做时,我还必须更新我的System.Spatial nuget包)。如果我使用命令“container.Receipts.Proof().GetValue()”,我就可以在服务器上对我的自定义函数执行它……从MVC控制器,您是否必须添加“*.GetValue()”以使它实际执行该函数?我原以为只有MyOdataClient.MyType.MyFunction(myStringParameterName)就足够了。当然,我对这个框架还是新手…函数名只是获取DataServiceQuery(一个包含上下文、RequestUri等的对象),但它不执行http调用,因为您可以在sync/async/etc中进行选择。。请随时检查Microsoft.Odata.Client.dll