.Net Core上的OData在$ select上未返回正确的结果 [英] OData on .Net Core doesn't return the right results on $select

查看:179
本文介绍了.Net Core上的OData在$ select上未返回正确的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将OData添加到我的WebAPI项目中.

I've added OData to my WebAPI project.

版本:

  1. Core 3.1
  2. OData 7.3.0(测试版,以便与Core 3.x一起使用)
  3. EF Core 3.1.0

这是我的startup.cs

Here is my startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddDbContext<Models.Contexts.EntityContext>(opts => opts.UseSqlServer(Configuration["ConnectionString:MailBackup"]));
        services.AddControllers();
        services.AddMvc(options =>
        {
            options.EnableEndpointRouting = false;
        }).SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_3_0);

        services.AddOData();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }

        app.UseMvc(routeBuilder =>
        {
            routeBuilder.EnableDependencyInjection();
            routeBuilder.Expand().Select().OrderBy().Filter();
        });

        app.UseHttpsRedirection();
        app.UseAuthorization();

    }
}

控制器是:

[EnableQuery()]
[HttpGet]
[Route("GetAll")]
public IQueryable<Models.EmailMessage> GetAll()
{    
    return this._context.EmailMessages;
}

API运行正常,但是当我尝试添加一些$$之类的OData操作时,得到的却不是预期的结果:

The APIs are working fine, but when I try to add some OData action like $select I get the following and not the expected results:

{
    "instance": null,
    "container": {},
    "modelID": "529e8054-04c4-4729-aa91-d7eaf67a55d0",
    "untypedInstance": null,
    "instanceType": null,
    "useInstanceForProperties": false
},
{
    "instance": null,
    "container": {},
    "modelID": "529e8054-04c4-4729-aa91-d7eaf67a55d0",
    "untypedInstance": null,
    "instanceType": null,
    "useInstanceForProperties": false
},

推荐答案

我遇到了同样的问题.这为我解决了问题.也许odata与Asp.Net 3.1中的新JSON序列化程序不完全兼容.我不知道.

I had the same problem. This solved the problem for me. Perhaps odata is not fully compatible with the new JSON serializer in Asp.Net 3.1. I don't know.

services.AddControllers(mvcOptions =>
           mvcOptions.EnableEndpointRouting = false)
       .AddNewtonsoftJson();

这篇关于.Net Core上的OData在$ select上未返回正确的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆