我想在我的Web API中同时使用OData和Swagger。我正在运行ASP.NET Core 3.1。
我找到了这些文章,其中一篇启用OData,另一篇启用SwaggerUI
但是,我似乎无法同时启用两者。看来我在混错他们。
这是我目前拥有的代码:
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.AddControllers();
services.AddOData();
AddSwagger(services);
}
// 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();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Foo API V1");
});
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.Select().Filter().OrderBy().Count().MaxTop(10);
endpoints.MapODataRoute("odata", "odata", GetEdmModel());
});
}
private IEdmModel GetEdmModel()
{
var odataBuilder = new ODataConventionModelBuilder();
odataBuilder.EntitySet<WeatherForecast>("WeatherForecast");
return odataBuilder.GetEdmModel();
}
private void AddSwagger(IServiceCollection services)
{
services.AddSwaggerGen(options =>
{
var groupName = "v1";
options.SwaggerDoc(groupName, new OpenApiInfo
{
Title = $"Foo {groupName}",
Version = groupName,
Description = "Foo API",
Contact = new OpenApiContact
{
Name = "Foo Company",
Email = string.Empty,
Url = new Uri("https://example.com/"),
}
});
});
}
}
当我转到https:// localhost:44363 / odata / weatherforecast时,它可以工作,但是当我尝试加载Swagger接口时,这表明:
它什么也没显示!
这是我的控制器:
[Route(“ [controller]”)]公共类WeatherForecastController:ControllerBase {私有静态只读字符串[]摘要= new [] {“冻结”,“拥抱”,“寒冷”,“酷”,“轻度”,“温暖” “,” Balmy“,” Hot“,” Sweltering“,” Scorching“};
[EnableQuery]
public IEnumerable<WeatherForecast> Get()
{
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Id = Guid.NewGuid(),
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]
})
.ToArray();
}
}
我在nuget包下面使用,此问题已解决。安装包OData.Swagger
参考:https : //github.com/KishorNaik/Sol_OData_Swagger_Support
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句