简体   繁体   中英

Is it possible to pass environment variables to an asp .net core application via IIS without saving them to the web.config?

I'm looking for a way to pass data to an asp.net core web application as environment variables.

You can do this via IIS via:

Management > Configuration Editor > environment variables > ...

However, that just writes the values into the web.config for the project:

<system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\Foo.exe" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
            <environmentVariables>
                <environmentVariable name="Foo" value="Bar" />
                <environmentVariable name="FooBar" value="1" />
            </environmentVariables>
        </aspNetCore>
  </system.webServer>

That's lovely and all, but unfortunately also quite useless, because if I have three websites, pointing to the same published folder, they now all share the same 'environment' variables.

...

Not really environment variables.

More like appsettings.

I looked into 'application settings' in IIS, but since kestrel runs in its own process, the code for the application instances is not managed code and that doesn't work either (note that using 'No managed code' or '...whatever CLR version here' in the application pool settings is irrelevant for .net core apps).

So, if you have multiple sites configured in IIS for an app, pointing to the same folder , is there any way to pass different configuration to each site instance from IIS?

I believe everything that you change in IIS is saved to the web.config. This is so that as a developer you don't actually have to set up the same configuration on every server the app is deployed to. Unfortunately this means that no, there is no way to do what you are asking.

You will need to publish to three separate folders so you can have three web.config files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM