Setting environment variables in .NET Core 2.0

后端 未结 4 722
傲寒
傲寒 2020-12-14 02:58

I am trying to setting up multiple environments in my .NET Core 2.0 application. See my code below.

Configuration file (Launch.JSON)

\"configuratio         


        
4条回答
  •  借酒劲吻你
    2020-12-14 03:27

    Finally I have done it...

    Let’s look at how I achieved this.

    1. I have added all my profile settings in launchSettings.JSON
    2. Program.cs remains same as I added in my question.
    3. Updated startup.cs (see below)
    4. CLI for run it via terminal is also different.

    Now first let's see my project structure.

    Code in my launchSettings.json

    {
      "iisSettings": {
        "windowsAuthentication": false,
        "anonymousAuthentication": true,
        "iisExpress": {
          "applicationUrl": "http://localhost:40088/",
          "sslPort": 0
        }
      },
      "profiles": {
        "Development": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          }
        },
        "Azuredev": {
          "commandName": "Project",
          "launchBrowser": true,
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Azuredev"
          }
        }
      }
    }
    

    Code in launch.json

    {
    "version": "0.2.0",
    "configurations": [
            {
                "name": ".NET Core Launch (web)",
                "type": "coreclr",
                "request": "launch",
                "preLaunchTask": "build",
                // If you have changed target frameworks, make sure to update the program path.
                "program": "${workspaceRoot}/my.api/bin/Debug/netcoreapp2.0/my.api.dll",
                "args": [],
                "cwd": "${workspaceRoot}/my.api",
                "stopAtEntry": false,
                "requireExactSource": false,
                "internalConsoleOptions": "openOnSessionStart",
                "launchBrowser": {
                    "enabled": true,
                    "args": "${auto-detect-url}",
                    "windows": {
                        "command": "cmd.exe",
                        "args": "/C start ${auto-detect-url}"
                    },
                    "osx": {
                        "command": "open"
                    },
                    "linux": {
                        "command": "xdg-open"
                    }
                },
                "sourceFileMap": {
                    "/Views": "${workspaceRoot}/Views"
                }
            },
            {
                "name": ".NET Core Attach",
                "type": "coreclr",
                "request": "attach",
                "processId": "${command:pickProcess}"
            }
        ]
    }
    

    startup.cs

        public IConfigurationRoot Configuration { get; }
    
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables();
    
            Configuration = builder.Build();
    
            this.HostingEnvironment = env;
        }
    

    After this all changes, my API is working fine with both the F5 debug option as well as CLI terminal.

    To launch the application from the command line, use these keywords:

    dotnet run --launch-profile "Development"
    

    OR

    dotnet run --launch-profile "Azuredev"
    

提交回复
热议问题