Reputation: 329
I can't understand how to teach to the web api and IIS to open the default page index.html of Swagger when navigate into. I'd like to redirect the default WebApi page to Swagger.
The WebApi is into "Default Web Site".
I tried the following:
if (app.Environment.IsDevelopment())
{
app.UseSwaggerUI();
}
else if (app.Environment.IsProduction())
{
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1.0/swagger.json", "v1.0");
options.RoutePrefix = "api/swagger";
});
}
IIS open the default "https://localhost/WebApi" showing
{"Message":"Value cannot be null. (Parameter \u0027key\u0027)","StatusCode":500}
Any suggestions?
Thanks
Upvotes: 1
Views: 14345
Reputation: 400
For me, this suggestion worked: https://stackoverflow.com/a/49298014/8225290
It adds a controller that performs a redirect for the URL "/" to "/swagger/index.html". I also added a redirect to "/index.html", because my browser seems to have cached this URL, but probably it is not necessary:
using Microsoft.AspNetCore.Mvc;
namespace My.Namespace
{
[ApiExplorerSettings(IgnoreApi = true)]
public class HomeController : Controller
{
[Route("/")]
[Route("/index.html")]
public IActionResult Index()
{
return new RedirectResult("~/swagger/index.html");
}
}
}
Upvotes: 0
Reputation: 1565
You can add the launchUrl to the launch.json file. You can configure a different launchUrl for every profile.
"TestWebApplication": {
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:5001;http://localhost:5000",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
Upvotes: 1
Reputation: 165
try to remove the if else block.
On my Startup.cs inside Configure method I have just
app.UseSwagger();
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API NAME 1.0.0.0"));
then inside ConfigureService method:
services.AddSwaggerGen(c =>
{
c.CustomSchemaIds(x => x.FullName);
c.SwaggerDoc("v1",
new OpenApiInfo { Title = "MyApi", Version = "v1" });
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);
});
it works on production and on development mode to with the follow url's
development:
production:
Upvotes: 0