OData 与 CosmosDb 的集成不会返回预期的结果

问题描述

我创建了一个与 Azure Cosmos Db 连接的 .Net core 3.1 WebApi 应用程序。 WebAPI 正确地从 CosmosDb 返回数据。当我尝试将 OData 集成到此解决方案中,并尝试使用 Select 方法查询数据时,它没有返回预期的结果。

以下是我的代码

StartUp.cs

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

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddOData();
            services.AddControllersWithViews();
            services.AddSingleton<ICosmosDbService>(InitializeCosmosClientInstanceAsync(Configuration.GetSection("CosmosDb")).GetAwaiter().GetResult());
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app,IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios,see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }
            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                   name: "default",pattern: "{controller=Todo}/{action=Index}/{id?}");

                endpoints.EnableDependencyInjection();
                endpoints.Select().Filter().OrderBy().Expand();
            });
        }

    }

WebAPI 控制器:

    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
    public class ItemsController : ControllerBase
    {
        private readonly ICosmosDbService _cosmosDbService;
        public ItemsController(ICosmosDbService cosmosDbService)
        {
            _cosmosDbService = cosmosDbService;
        }

        // GET: api/<ItemsController>
        [HttpGet]
        [EnableQuery()]
        public async Task<IEnumerable<Item>> Get()
        {
            return await _cosmosDbService.GetItemsAsync("SELECT * FROM c");
        }
   }

当我尝试使用 API 调用(https://localhost:44357/api/items)检索数据时,我得到了预期的结果:

[{"id":"5f4f5d02-9217-4591-8f8c-2af9fe7d9ae4","name":"Brush","description":"每晚刷牙","completed":true,"partitionKey" :null},{"id":"6a5edfe3-9c84-4398-bed4-963dbb4a42e3","name":"Excercise","description":"晚上去健身房","partitionKey ":null}]

但是当我尝试使用 OData 方法(https://localhost:44357/api/items?$select=name)时,我没有得到预期的结果。相反,我得到了这个:

[{"instance":null,"container":{},"modelID":"7c0ae376-1666-46f8-886f-9bf758824063","untypedInstance":null,"instanceType":null,"useInstanceForProperties": false},{"instance":null,"useInstanceFor{{1 }}属性":false}]

知道为什么会这样吗?任何帮助将不胜感激。谢谢

解决方法

存在与 Asp.Net 3.1 中的 JSON 序列化程序不兼容的情况。尝试AddNewtonsoftJson

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...