.Net Core 3.1 API 在使用 Asp.NetCore.Odata for REST API 时遇到问题
.NetCore 3.1 API having issues using Asp.NetCore.Odata for REST API
我想做的主要事情是我希望我的 AspNet.Core 3.1 API 支持 Odata。最主要的是 WebApi 暴露了内部映射到实体对象的 Dto 对象。我已经实施了更改,但不断收到 404 错误。我所做的更改是:
安装AspNetCore.OData7.3.
对 Startup.cs
添加了更改
public class 启动
{
public void ConfigureServices(IServiceCollection 服务)
services.AddOData();
services.AddMvc(选项 => options.EnableEndpointRouting = 假);
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
IEdmModel model = GetEdmModel();
app.UseMvc(builder =>
{
builder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
builder.MapODataServiceRoute("odata", "odata", model);
});
public static IEdmModel GetEdmModel()
{
if (_edmModel == null)
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<CustomerDto>("CustomerDtos");
_edmModel = builder.GetEdmModel();
}
return _edmModel;
}
}
- 客户控制器更改
public class CustomersController : ControllerBase
{
public ICustomerRepository _customers;
public CustomersController(DbContext context)/
{
_customers = new CustomerRepository(context);
}
[HttpGet]
[EnableQuery]
public async Task<ActionResult<IEnumerable<CustomerDto>>> Get()
{
return Ok((await _customers.GetCustomers()).Select(c=>CustomerDto.MapToCustomerDto(c)));
}
}
但是这会不断出现 404 错误 odata/customers
如果你使用url/odata/customers
,customers
指的是一个名为Customers
的实体集。控制器名称始终派生自 OData 路径根目录下的实体集。
解决方案:
一种方法是保留 EntitySet 名称但修改控制器名称
(CustomersController->CustomerDtosController):
public class CustomerDtosController : ControllerBase
Url: /odata/customerDtos
另一种方法是保留控制器名称但修改 EntitySet
名称:
builder.EntitySet<CustomerDto>("Customers");
Url: odata/customers
我想做的主要事情是我希望我的 AspNet.Core 3.1 API 支持 Odata。最主要的是 WebApi 暴露了内部映射到实体对象的 Dto 对象。我已经实施了更改,但不断收到 404 错误。我所做的更改是:
安装AspNetCore.OData7.3.
对 Startup.cs
添加了更改
public class 启动 { public void ConfigureServices(IServiceCollection 服务) services.AddOData(); services.AddMvc(选项 => options.EnableEndpointRouting = 假);
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
IEdmModel model = GetEdmModel();
app.UseMvc(builder =>
{
builder.Select().Expand().Filter().OrderBy().MaxTop(100).Count();
builder.MapODataServiceRoute("odata", "odata", model);
});
public static IEdmModel GetEdmModel()
{
if (_edmModel == null)
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<CustomerDto>("CustomerDtos");
_edmModel = builder.GetEdmModel();
}
return _edmModel;
}
} - 客户控制器更改
public class CustomersController : ControllerBase
{
public ICustomerRepository _customers;
public CustomersController(DbContext context)/
{
_customers = new CustomerRepository(context);
}
[HttpGet]
[EnableQuery]
public async Task<ActionResult<IEnumerable<CustomerDto>>> Get()
{
return Ok((await _customers.GetCustomers()).Select(c=>CustomerDto.MapToCustomerDto(c)));
}
}
但是这会不断出现 404 错误 odata/customers
如果你使用url/odata/customers
,customers
指的是一个名为Customers
的实体集。控制器名称始终派生自 OData 路径根目录下的实体集。
解决方案:
一种方法是保留 EntitySet 名称但修改控制器名称
(CustomersController->CustomerDtosController):
public class CustomerDtosController : ControllerBase
Url: /odata/customerDtos
另一种方法是保留控制器名称但修改 EntitySet
名称:
builder.EntitySet<CustomerDto>("Customers");
Url: odata/customers