仅具有 ABP 的 OData returns 实体为 404

OData with ABP only returns 404 for entities

我正在使用 ABP 和 OData 开发 .NET Core 2.2 网络应用程序,并且我已经关注 this guide。设置好所有内容后,我可以查询 http://localhost:21021/odata 并得到以下结果:

{"result":{"entitySets":[{"url":"Empresas","name":"Empresas","title":null,"typeAnnotation":null},{"url":"Users","name":"Users","title":null,"typeAnnotation":null}],"singletons":[],"functionImports":[],"typeAnnotation":null},"targetUrl":null,"success":true,"error":null,"unAuthorizedRequest":false,"__abp":true}

但是我对我的实体所做的任何尝试,比如点击 http://localhost:21021/odata/Empresas,只会导致 404。

可能是什么问题?

Startup.cs

    public IServiceProvider ConfigureServices(IServiceCollection services)
    {
        // MVC
        services.AddMvc(
            options => options.Filters.Add(new CorsAuthorizationFilterFactory(_defaultCorsPolicyName))
        );

        services.AddOData();

        // Workaround: https://github.com/OData/WebApi/issues/1177
        services.AddMvcCore(options =>
        {
            foreach (var outputFormatter in options.OutputFormatters.OfType<ODataOutputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                outputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
            foreach (var inputFormatter in options.InputFormatters.OfType<ODataInputFormatter>().Where(_ => _.SupportedMediaTypes.Count == 0))
            {
                inputFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
            }
        });

        IdentityRegistrar.Register(services);
        AuthConfigurer.Configure(services, _appConfiguration);
        // ... SignalR, CORS, Swagger, etc ...

        return services.AddAbp<EcfWebHostModule>(
            // Configure Log4Net logging
            options => options.IocManager.IocContainer.AddFacility<LoggingFacility>(
                f => f.UseAbpLog4Net().WithConfig("log4net.config")
            )
        );

    public void Configure(IApplicationBuilder app)
    {
        app.UseAbp(options => { options.UseAbpRequestLocalization = false; }); // Initializes ABP framework.

        // ... UseCors, UseStaticFiles, UseAuthentication, UseAbpRequestLocalization, UseSignalR ...

        app.UseOData(builder =>
        {
            builder.EntitySet<EmpresaEntity>("Empresas").EntityType.Expand().Filter().OrderBy().Page();
            builder.EntitySet<User>("Users").EntityType.Expand().Filter().OrderBy().Page();
        });

        app.UseUnitOfWork(options =>
        {
            options.Filter = httpContext =>
            {
                return httpContext.Request.Path.Value.StartsWith("/odata");
            };
        });

        // ... Swagger ...

        app.UseMvc(routes =>
        {
            routes.MapODataServiceRoute(app);

            routes.MapRoute(
                name: "defaultWithArea",
                template: "{area}/{controller=Home}/{action=Index}/{id?}");

            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

EmpresaController.cs

public class EmpresaController : AbpODataEntityController<EmpresaEntity>, ITransientDependency
{
    public EmpresaController(IRepository<EmpresaEntity> repository)
        : base(repository)
    {

    }
}

EmpresaEntity.cs

public sealed class EmpresaEntity : FullAuditedEntity
{
    public string Cnpj { get; set; }
    public string Nome { get; set; }
}

终于解决了

只需更改控制器的名称以匹配在启动时作为参数传递的字符串 class。文档中并不清楚它是如何工作的。

builder.EntitySet<EmpresaEntity>("Empresas").EntityType.Expand().Filter().OrderBy().Page();

EmpresasController