gpt4 book ai didi

c# - 尝试访问 swagger 端点时,将 Nswag 与 Odata 结合使用会产生错误

转载 作者:行者123 更新时间:2023-12-02 03:01:33 30 4
gpt4 key购买 nike

我们正在尝试在我们的 asp.net 核心 API 项目中同时使用 Nswag 和 Odata。我们可以使用 Nswag 来获取 API 文档,也可以使用 Odata 来简化查询。但是当我们同时使用它们并尝试访问 API swagger 文档 (https://localhost:5001/swagger/index.html) 时,它会生成此错误: Error in Nswag and Odata

这是我的启动文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;


using Microsoft.AspNet.OData.Extensions;
using System.Web.Http;
using Microsoft.OpenApi.Models;
using System.Reflection;
using Newtonsoft.Json;
using Microsoft.AspNet.OData.Builder;
using GL.Data.Models.EntityClass;
using Microsoft.Net.Http.Headers;
using Microsoft.AspNet.OData.Formatter;
using Newtonsoft.Json.Serialization;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
using Newtonsoft.Json.Converters;

namespace GL.service
{
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.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(options =>
{
// Use camel case properties in the serializer and the spec (optional)
options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
// Use string enums in the serializer and the spec (optional)
options.SerializerSettings.Converters.Add(new StringEnumConverter());
});

// registers a Swagger v2.0 document with the name "v1" (default)
services.AddSwaggerDocument(c => {
c.DocumentName = "V1";
c.Title = "GL Controller";
});
services.AddOData();

}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();

}
else
{

app.UseHsts();
}
app.UseCors(b => b.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials());
app.UseHttpsRedirection();
app.UseAuthentication();

app.UseOpenApi(); // Serves the registered OpenAPI/Swagger documents by default on
app.UseSwaggerUi3(); // Serves the Swagger UI 3 web ui to view the OpenAPI/Swagger
app.UseMvc(routeBuilder =>
{
routeBuilder.EnableDependencyInjection();
routeBuilder.Expand().Select().Filter().Count().OrderBy();
});
}
}
}

请帮助解决这个问题。

最佳答案

这是一个workaround您可以在加载 NSwag UI 时修复 API 错误。但是 Swashbuckle for AspNetCore 不支持 OData,并且您的 NSwag UI 中不会显示任何 OData 端点。

services.AddOData();

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"));
}
});

请记住将 services.AddOData(); 放在 services.AddMvcCore() 行之前。

引用https://github.com/OData/WebApi/issues/1177

关于c# - 尝试访问 swagger 端点时,将 Nswag 与 Odata 结合使用会产生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59853954/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com