C# Web API和OData-传递多个参数
有可能让OData执行以下操作吗?我希望能够通过传递可能不是主键的参数来查询REST调用。 我可以调用像-->C# Web API和OData-传递多个参数,c#,odata,asp.net-web-api2,C#,Odata,Asp.net Web Api2,有可能让OData执行以下操作吗?我希望能够通过传递可能不是主键的参数来查询REST调用。 我可以调用像-->GetReports(22,2014)或Reports(22,2014)这样的REST方法吗 这是我的最新变化。 //Unbound Action OData v3 var action = builder.Action("ListReports"); action.Parameter<int>("key"); action
GetReports(22,2014)
或Reports(22,2014)
这样的REST方法吗
这是我的最新变化。
//Unbound Action OData v3
var action = builder.Action("ListReports");
action.Parameter<int>("key");
action.Parameter<int>("year");
action.ReturnsCollectionFromEntitySet<Report>("Reports");
我试着像这样调用url:
http://localhost:6064/odata/Reports(key=5,year=2014)/ListReports
未找到与请求URI“
http://localhost:6064/odata/Reports(键%3D5%2Cyear%3D2014)/ListReports'
`您可以定义一个名为GetReports的函数导入,它有两个参数
(注意:函数导入的名称不能与实体集名称相同)
将您的EDM模型配置为:
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Report>("Reports");
var function = builder.Function("GetReports");
function.Parameter<int>("Id");
function.Parameter<int>("Year");
function.ReturnsCollectionFromEntitySet<Report>("Reports");
var model = builder.GetEdmModel();
然后请求
Get ~/GetReports(Id=22,Year=2014)
将起作用。对于OData v4.0端点,您不必将其设置为函数,只需执行以下操作即可
public class ReportsController : ODataController
{
[EnableQuery]
[ODataRoute("Reports({id}, {year})")]
public IQueryable<ReportModel> Get([FromODataUri] int id, [FromODataUri] int year)
{
...
}
}
如何使用Odatav3实现这一点?v3没有函数。我必须使用Odata v3,因为我使用的是Jaydata,而且它的sems仅用于v3。如果将Jaydata与Odata v4一起使用,则会引发404错误。使用v3的任何解决方案?感谢使用v3,您可以定义操作。如果定义未绑定操作,则必须使用HttpPost并通过请求正文传递参数,并且需要在控制器中添加方法“HandleUnmappedRequest”以匹配未绑定操作请求。如果您将其定义为绑定到实体集的绑定操作(尽管这没有意义),您可以添加一个方法“[EntityTypeName]OnCollectioinOf[EntityTypeName]”并调用HttpPost“~/[EntitySetName]/[ActionName]”,并在请求正文中传递参数。谢谢。你有什么地方可以给我举个例子吗?有没有显示如何创建未绑定操作的博客?v3操作示例上有许多示例,请检查此项
[HttpGet]
[ODataRoute("GetReports(Id={Id},Year={Year})")]
public IHttpActionResult WhateverName([FromODataUri]int Id, [FromODataUri]int Year)
{
return Ok(_reportsRepository.GetReports(Id, Year));
}
Get ~/GetReports(Id=22,Year=2014)
public class ReportsController : ODataController
{
[EnableQuery]
[ODataRoute("Reports({id}, {year})")]
public IQueryable<ReportModel> Get([FromODataUri] int id, [FromODataUri] int year)
{
...
}
}
/Reports(42, 2019)