.NetCore 3.1 API 在将 Asp.NetCore.Odata 用于 REST API 时遇到问题

深的

我想做的主要事情是我希望我的 AspNet.Core 3.1 API 支持 Odata。主要的是 WebApi 公开了 Dto 对象,该对象在内部映射到实体对象。我已经实施了更改,但是不断收到 404 错误。我所做的改变是:

  • 安装 AspNetCore.OData 7.3。

  • 添加了对 Startup.cs 的更改

public class Startup { public void ConfigureServices(IServiceCollection services) services.AddOData(); services.AddMvc(options => options.EnableEndpointRouting = false);

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 路径根部的实体集。

请参阅https://docs.microsoft.com/en-us/odata/webapi/built-in-routing-conventions#built-in-routing-conventions-1

解决方案

一种方法是保留 EntitySet 名称但修改控制器名称

(CustomersController->CustomerDtosController ):

public class CustomerDtosController : ControllerBase

网址: /odata/customerDtos

另一种方法是保留控制器名称但修改EntitySet名称:

builder.EntitySet<CustomerDto>("Customers");

网址: odata/customers

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

提交到REST API时遇到问题-仅适用于Firefox

从 NetCore 2 Newtonsoft JSON 移动到 NetCore 3 中的新 JSON API 时添加选项

用于 ASP.NET Core 和 OData 的 REST Web API 中的 IQueryable、IEnumerable 和异步

将.NetCore3.1 API部署到LinuxCentos(Nginx)

如何将 qrcode 从 .netcore api 传递到 angular

Odata $ filter用于Office 365 REST API中的日期

.NetCore 3.0 Web API AzureAD身份验证问题

在React中无法从api(Django Rest Framework)响应渲染图像数组时遇到问题

交互读取Neo4j Rest API查询结果时遇到问题

在 Asp.net web api 中通过 id 查找用户时遇到问题

将匹配数据列表从 riot api 保存到状态数组时遇到问题

使用HTTPie将新用户发布到Rails API时遇到问题

使用其余API将angular js与wordpress集成时遇到问题

使用indexeddb jQuery API将数据插入indexeddb时遇到问题

将 API 调用更新为 64 位时遇到问题

asp.net中的oData,REST和Web API

如何捆绑.NetCore API + Angular前端

使用 .netcore 从 web api 获取 JSON 数据

在Netcore API 3.1中读取AuthorizationFilterContext

.NetCore SignalR通过API调用加入组

.NetCore Api总是找不到404

AspNetZero .NetCore + Angular 项目 - api 版本控制

将OAuth用于Spring Rest API

将SOAP UI用于Marklogic SPARQL REST API时出现问题

在 Asp.NetCore Web API 中为电子邮件使用 foreach for C#

在将“ GoDebug”插件用于Sublime时遇到问题

将c ++ / cli项目从.net 4.7迁移到.netcore 3.1时遇到的问题

尝试使用Mono和Nginx在Debian上托管ASP.NET Web Api时遇到问题

在 C# 中创建 Asp.net Web Api 时遇到问题,详细信息如下: