且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将ASPNETCORE_ENVIRONMENT设置为发布asp.net核心应用程序?

更新时间:2022-10-28 18:08:23

Option1:



要在Windows中设置ASPNETCORE_ENVIRONMENT环境变量, b
$ b

命令行 - setx ASPNETCORE_ENVIRONMENT开发



PowerShell - $ Env:ASPNETCORE_ENVIRONMENT =开发



对于其他操作系统,请参阅 - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments



Option2:



如果要使用设置ASPNETCORE_ENVIRONMENT web.config 然后添加 aspNetCore 这样 -

 <结构> 
<! -
在appsettings.json中配置您的应用程序设置。了解更多信息,请访问http://go.microsoft.com/fwlink/?LinkId=786380
- >
< system.webServer>
<处理程序>
< add name =aspNetCorepath =*verb =*modules =AspNetCoreModuleresourceType =未指定/>
< / handlers>
< aspNetCore processPath =。\MyApplication.exearguments =stdoutLogEnabled =falsestdoutLogFile =。\logs\stdoutforwardWindowsAuthToken =false>
< environmentVariables>
< environmentVariable name =ASPNETCORE_ENVIRONMENTvalue =开发/>
< / environmentVariables>
< / aspNetCore>
< /system.webServer>
< / configuration>


when i publish my asp.net core web application to my local file system, it always takes the production-config and the ASPNETCORE_ENVIRONMENT variable with the value = "Production".

how and where do i have to set the value of the ASPNETCORE_ENVIRONMENT variable so that it will be considered not only for debugging, but also for the publishing? i already tried the following options without success:

  • in windows settings
  • in .pubxml file
  • in launchSettings.json
  • in project.json

Option1:

To set the ASPNETCORE_ENVIRONMENT environment variable in windows,

Command line - setx ASPNETCORE_ENVIRONMENT "Development"

PowerShell - $Env:ASPNETCORE_ENVIRONMENT = "Development"

For other OS refer this - https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments

Option2:

If you want to set ASPNETCORE_ENVIRONMENT using web.config then add aspNetCore like this-

<configuration>
  <!--
    Configure your application settings in appsettings.json. Learn more at http://go.microsoft.com/fwlink/?LinkId=786380
  -->
  <system.webServer>
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath=".\MyApplication.exe" arguments="" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables>
        <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
      </environmentVariables>
    </aspNetCore>
  </system.webServer>
</configuration>