【发布时间】:2018-08-12 22:17:49
【问题描述】:
我正在尝试为客户实现一个 ODataController,它能够为我提供查询模型的 uri
http://localhost:1234/odata/v1/customers?$top=4
或使用以下方法根据 Id 获得一位客户
http://localhost:1234/odata/v1/customers/1
但无论我尝试什么,我都无法将参数传递给控制器上的操作/功能。
我的代码是这样的。
app.UseMvc(routeBuilder =>
{
routeBuilder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
routeBuilder.MapODataServiceRoute("ODataRoutes", "odata/v1", modelBuilder.GetEdmModel(app.ApplicationServices));
routeBuilder.EnableDependencyInjection();
});
GetEdmModel 基本上是这样构建模型的:
builder.EntitySet<Customer>("Customers")
.EntityType
.Filter() // Allow for the $filter Command
.Count() // Allow for the $count Command
.Expand() // Allow for the $expand Command
.OrderBy() // Allow for the $orderby Command
.Page() // Allow for the $top and $skip Commands
.Select()// Allow for the $select Command;
.ContainsMany(x => x.Transactions)
.Expand();
在控制器本身我已经定义了属性路由
[Produces("application/json")]
[ODataRoutePrefix("v1/[controller]")]
public class CustomersController : ODataController
{
[EnableQuery]
public ActionResult<IQueryable<Customer>> GetCustomers()
{
try
{
return context.Set<Customer>();
}
catch (Microsoft.OData.ODataException ex)
{
return BadRequest(ex.Message);
}
}
[EnableQuery]
public ActionResult<Customer> Get([FromODataUri] string id)
{
try
{
var customer = context.Set<Customer>().Where(r => r.CustomerId == id).SingleOrDefault();
我看到当我有 uri = http://localhost:1234/odata/v1/customers/1 时,它会传递给函数 Get([FromODataUri] string id) 但是 id 的值始终为空。我已经尝试定义[ODataRoute("{id}")],但即使这样也没有用。
【问题讨论】:
标签: .net-core odata asp.net-core-2.1