简体   繁体   English

是否可以通过IIS将环境变量传递到asp .net核心应用程序而不将其保存到web.config?

[英]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. 我正在寻找一种将数据作为环境变量传递到asp.net核心Web应用程序的方法。

You can do this via IIS via: 您可以通过以下方式通过IIS执行此操作:

Management > Configuration Editor > environment variables > ...

However, that just writes the values into the web.config for the project: 但是,这只是将值写入项目的web.config中:

<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. 更像是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). 我在IIS中研究了“应用程序设置”,但是由于kestrel在其自己的进程中运行,因此应用程序实例的代码不是托管代码,并且也不起作用(请注意,使用“无托管代码”或“ ... .net核心应用程序与应用程序池设置中的此处的CLR版本无关)。

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? 因此,如果您在IIS中为一个应用程序配置了多个站点,并指向同一文件夹 ,是否有任何方法可以将不同的配置从IIS传递到每个站点实例?

I believe everything that you change in IIS is saved to the web.config. 我相信您在IIS中更改的所有内容都会保存到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. 您将需要发布到三个单独的文件夹,以便可以拥有三个web.config文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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