Web API 方法在添加 OData 功能后未获取数据

Web API method not fetching data after adding OData functionality

我正在使用 ASP.NET Core 2.1 Web API 和 Angular 6 作为前端。

我已将 OData 功能添加到 Web API 项目中的 Startup.cs 文件。我还使用 Swagger 来记录 API.

这是 Startup.cs 文件中 ConfigureServices 的代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddOData();
    services.AddCors();

    services.AddMvc(opt =>
    {
        opt.UseGeneralRoutePrefix("BookManagement");
        opt.SslPort = SettingsLoader<AppSettings>.Settings.SslPort;
    }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1); ;

    services.AddMvcCore(options =>
    {
        foreach (var outputFormatter in options.OutputFormatters.OfType<OutputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
        {
            outputFormatter.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
        }

        foreach (var inputFormatter in options.InputFormatters.OfType<InputFormatter>().Where(x => x.SupportedMediaTypes.Count == 0))
        {
            inputFormatter.SupportedMediaTypes.Add(new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/prs.odatatestxx-odata"));
        }
    });

    services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

    services.AddRequestScopingMiddleware(() => _scopeProvider.Value = new Scope());
    services.AddCustomControllerActivation(Resolve);
    services.AddCustomViewComponentActivation(Resolve);


    // Register the Swagger generator, defining 1 or more Swagger documents
    services.AddSwaggerGen(c =>
    {
        c.SwaggerDoc($"v{AppVersion.cVersion}", new Info { Title = "Books Microservice", Version = $"v{AppVersion.cVersion}" });
        c.IncludeXmlComments(Path.Combine(Environment.CurrentDirectory, "Synergy.AssetsModule.WebApi.xml"));
    });
}

Configure方法如下

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    Kernel = RegisterApplicationComponents(app);

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }

    if (SettingsLoader<AppSettings>.Settings.AutoUpdateDatabase)
    {
        IDatabaseLogic databaseLogic = new DatabaseLogic(Kernel.Get<IDatabasePersistence>());
        databaseLogic.UpgradeDatabase();
    }
    InitiateMessageReprocess();

    #if !NO_AUTH
        ISingleSignOnLogic singleSignOnLogic = Kernel.Get<ISingleSignOnLogic>();
        app.UseMiddleware<AuthenticationFilter>(singleSignOnLogic);
    #endif

    // Enable middleware to serve generated Swagger as a JSON endpoint.
    app.UseSwagger();

    // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), 
    // specifying the Swagger JSON endpoint.
    app.UseSwaggerUI(c =>
    {
        c.SwaggerEndpoint($"/swagger/v{AppVersion.cVersion}/swagger.json", $"Books Microservice API v{AppVersion.cVersion}");
        c.RoutePrefix = string.Empty;
    });

    app.UseCors(builder =>
    {
        builder.AllowAnyOrigin();
        builder.AllowAnyMethod();
        builder.AllowAnyHeader();
    });

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

    app.UseRewriter(new RewriteOptions().AddRedirectToHttps(StatusCodes.Status301MovedPermanently, SettingsLoader<AppSettings>.Settings.SslPort));

    LogProvider.Instance.LogInformation("Started application");
}

使用此代码,Swagger API 返回 204 Undocumented。

不知道哪里出错了。我之前使用的参数内容类型是application/json-patch+json。请提出为什么我没有从 Web API 方法获取数据。

几件事。首先,我建议创建一个单独的 Web 服务来托管您的 OData 端点。其次,在 .net 6 之前,让 OData 和 Swagger 一起工作有点困难。即使在 .net 6 中,在 swagger 方面也缺少一些东西。

我有一个开源项目可以填补空白。这是一个演示如何使用它的示例项目。

https://github.com/ikemtz/NRSRx/tree/master/samples/IkeMtz.Samples.OData