我正在尝试在Odata webservice上执行自定义函数(在这种情况下,它是Proof())。我试图弄清楚我需要使用哪个URL来从客户端执行此控制器Proof()方法。这显然是错误映射的,因为我没有看到在调试模式下运行并进行客户端调用时执行的方法(_http:// localhost:53610 / Receipt / AccountsReceivableWebService.Proof())。
我应该使用什么URL从客户端执行此方法?更好的是,如何从WebApiConfig中正确映射此路径?批评是最受欢迎的!提前谢谢。
网络服务控制器如下:
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();
}
}
}
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());
}
}
}
编辑:我收到的错误是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>
答案 0 :(得分:0)
这是我在我的一个项目
中使用有界集合函数的方法在WebApi.config中
var function = customerBuilder.EntityType<MyType>().Collection.Function("MyFunction");
function.Returns<MyReturnType>();
function.Parameter<string>("myStringParameterName");
在我的MyTypeController中:ODataController
[HttpGet]
public IHttpActionResult MyFunction(string myStringParameterName)
{
//DBContextStuff
return Ok(new MyReturnType())
}
我在MVC控制器中使用数据
var myData = MyODataClient.MyType.MyFunction(myStringParameterName).GetValue();
希望它有所帮助。