mlst
mlst

Reputation: 3431

How to enable Swagger on remote dev environment in ASP.NET Core Web API app?

When developing on my local machine I am working under Development environment. The code that enables swagger is thus available with this condition:

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

I want to enable swagger on remote hosted server that is also used as development for other members of the team. However I don't want to use other local development environment variables localted in appsettings.Development.json such as db connection string.

What is the best way to set this up? How should I differentiate between local and remote dev environment so that I can enable swagger on both but keep database connection string separate.

Upvotes: 1

Views: 3245

Answers (2)

SDHEER
SDHEER

Reputation: 46

I believe removing the if statement will do this.

Upvotes: 0

B-Brazier
B-Brazier

Reputation: 121

If You have access to the Remote Server and Admin privileges (Assuming it is Windows) you need to set a system environment variable:

  • Variable Name: ASPNETCORE_ENVIRONMENT
  • Variable Value: Development

To do this Search Environment Variables. This will open a System Properties window and under the Advanced Tab click on Environment Variables...

Create a new System Environment Variable with the above details

Environment Variables

If you don't have access to the remote server try and get in contact with someone who does.

Hope this helps.

Alternatively you can create a custom environment variable as the value and add it into your C# project.

https://learn.microsoft.com/en-us/aspnet/core/fundamentals/environments?view=aspnetcore-7.0

This should avoid the appsettings.Development.json file being picked-up on build.

Upvotes: 1

Related Questions