Adding swagger to blank ASP.NET+React solution
Good day.
I am creating just a blank solution by codegenerator by command
dotnet new react -o MyReactApp
and I am trying to add Swagger here. So I installed nugets and edited Program.cs. OS is Windows 10, .NET 7.
Progam.cs now is
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });
});
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
} else
{
/*app.MapEndpoints(endpoints =>
{
// ...
endpoints.MapSwagger();
});*/
app.MapSwagger();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("v1/swagger.json", "My API V1");
});
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.MapControllerRoute(
name: "default",
pattern: "{controller}/{action=Index}/{id?}");
app.MapFallbackToFile("index.html");
app.Run();
and I do not see any Swagger. I am trying to run usual URL at localhost, adding '/swagger' at the end. Nothing is happen, and I see in client console that this URL does not exist. I have a feeling (correct or not), that react-router (that is used here) is intercepting URL to swagger.
What should I do?
I have discovered, that I had exactly same problem as here
500 Error when setting up Swagger in asp .net CORE / MVC 6 app
As it is explained at that topic, it is possible to get exact explanation, what "Internal server error" means in the debugger console (of Chrome, for example). My problem came from outside, I forgot to write [HttpGet] to another controller.