C
C#2y ago
Pokey

❔ Disable Swagger index.html Routing

I have just added and configured Swagger on my project with a custom rote, however, when launching my app on Debug, Swagger has taken over the root route / and redirects it to /index.html which is 404 because of my custom setup. How can I tell swagger to quit redirecting this and to leave it alone, and let my other routes take over (as I have a controller mapped to /)?
18 Replies
SwaggerLife
SwaggerLife2y ago
Show us the config. It's going to be hard, helping you like this.
Pokey
Pokey2y ago
Sure, I just assumed that it was a default function in Swagger One second
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("Developers", new OpenApiInfo
{
Title = "Open Radio Suite Orchestrator API",
Version = "API"
});

options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
});
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("Developers", new OpenApiInfo
{
Title = "Open Radio Suite Orchestrator API",
Version = "API"
});

options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, $"{Assembly.GetExecutingAssembly().GetName().Name}.xml"));
});
Then
app
.UseSwagger(options =>
{
options.RouteTemplate = "Api/{documentName}/api.json";
})
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api";
});
app
.UseSwagger(options =>
{
options.RouteTemplate = "Api/{documentName}/api.json";
})
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api";
});
When I access /Api I get swagger as desired. When I access / however, I get redirected to /index.html which is absolutely not what I want Ultimately, what I have works fine, just it insists on overriding my routing for /
SwaggerLife
SwaggerLife2y ago
Comment out route prefix and see what happens.
Pokey
Pokey2y ago
Well it's certainly stopped it from redirecting, however it has broken Swagger now
SwaggerLife
SwaggerLife2y ago
try this
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api/Developers/";
});
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api/Developers/";
});
Pokey
Pokey2y ago
Threw, Request path must not end in a slash removed
SwaggerLife
SwaggerLife2y ago
remove the end / from prefix
Pokey
Pokey2y ago
Now /Api is 404, /Api/Developers returns a broken swagger, but this is not what I wanted to achieve particularly
Pokey
Pokey2y ago
Pokey
Pokey2y ago
I do not ever intend to have multiple documents, hence I just want /Api to map to swagger
SwaggerLife
SwaggerLife2y ago
After searching for some solutions it seems like you need to change launchSettings
Pokey
Pokey2y ago
Nope, unfortunately not I couldn't care less how it launches The routing itself is broken
SwaggerLife
SwaggerLife2y ago
you wanna give it a try? try changing the launchUrl in launchSettings from swagger to Api/Developers
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api/Developers";
});
.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("Api/Developers/api.json", "Open Radio Suite Orchestrator API");
options.RoutePrefix = "Api/Developers";
});
Pokey
Pokey2y ago
I have no launchUrl at all The project was not created with Swagger It launches at / Swagger is mapping / to a redirect to /index.html and that overrides my controller mapped at /
SwaggerLife
SwaggerLife2y ago
Hm, then I'm not sure what's the issue mate. Sorry if I wasn't of help
Pokey
Pokey2y ago
Pokey
Pokey2y ago
No worries. What I had originally works fine Just Swagger being an arse Thank you
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.