Home > database >  ASP.NET Core 6 launchUrl equivalent for Production
ASP.NET Core 6 launchUrl equivalent for Production

Time:01-07

I'm using the ASP.NET Core 6 weather forecast sample. In my development environment, I can run the app and it shows the "default" page as localhost:xxxx/swagger/index.html.

In production, the default doesn't show; I can access it if I use the full URL https://example.com/swagger

How do I get ASP.NET Core 6 (in production) to default to that page?

I have tried:

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute("default", "{controller=swagger}");
});

It doesn't work either. Again the problem is with production. -- launchUrl works only in dev (by design)

CodePudding user response:

LunchSettings.json

    "ProductionEnvironment": {
       //Some configuration...

       //add this
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Production"
       }
     }

Program.cs

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();

}
//add Production Environment
else if (app.Environment.IsProduction())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

When you add the above configuration , The app will show the "default" page as localhost:xxxx/swagger/index.html in Production.

CodePudding user response:

I resolved the issue using the following code:

app.UseRouting(); 

app.UseSwagger();
app.UseSwaggerUI();

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
         context.Response.Redirect("swagger");

    });
});

It now works as expected.

  •  Tags:  
  • Related