Я хочу использовать в своем веб-API и OData, и Swagger. Я использую ASP. NET Core 3.1.
Я нашел эти статьи, одну для включения OData, а другую для включения SwaggerUI
Однако я могу ' Похоже, что оба этих параметра работают одновременно. Кажется, я неправильно их смешиваю.
Это код, который у меня сейчас есть:
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.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/"),
}
});
});
}
}
Он работает, когда я go к https://localhost: 44363 / odata / weatherforecast Но когда я пытаюсь загрузить интерфейс Swagger, это показывает:
![enter image description here](https://i.stack.imgur.com/zCZYO.png)
Он ничего не показывает!
Это мой контроллер:
Контроллер
[Route ("[controller]")] publi c class WeatherForecastController: ControllerBase {private stati c readonly string [] Summaries = new [] {"Замораживание", "Бодрящий", "Холодный", "Прохладный", "Мягкий", "Теплый", "Мягкий", "Горячий", "Душный" "," Обжигающий "};
[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();
}
}