Asp.net core 发布期间如何设置ASPNETCORE_环境?

Asp.net core 发布期间如何设置ASPNETCORE_环境?,asp.net-core,asp.net-core-mvc,Asp.net Core,Asp.net Core Mvc,我有几个WebDeploy发布配置文件,它们将我的.NET核心web项目部署到不同的位置(开发人员、QA、IIS上的后台)。为了让应用程序知道它在哪里运行,我需要设置ASPNETCORE_环境变量 是否可以将ASPNETCORE_环境变量设置为发布应用程序的一部分 p.S.并不能为我解决任何问题,因为它展示了如何手动实现它。我希望它能够自动作为发布配置文件的一部分 要使用特定环境发布web.config,您可以尝试 对于您的需求,您可以尝试Profile 使用不同的ASPNETCORE\u环境变

我有几个WebDeploy发布配置文件,它们将我的.NET核心web项目部署到不同的位置(开发人员、QA、IIS上的后台)。为了让应用程序知道它在哪里运行,我需要设置ASPNETCORE_环境变量

是否可以将ASPNETCORE_环境变量设置为发布应用程序的一部分


p.S.并不能为我解决任何问题,因为它展示了如何手动实现它。我希望它能够自动作为发布配置文件的一部分

要使用特定环境发布
web.config
,您可以尝试

对于您的需求,您可以尝试
Profile

  • 使用不同的
    ASPNETCORE\u环境变量创建多个web.{profile}.config

    例如,
    web.Dev.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location>
        <system.webServer>
        <aspNetCore>
            <environmentVariables xdt:Transform="InsertIfMissing">
            <environmentVariable name="ASPNETCORE_ENVIRONMENT"
                                value="Dev"
                                xdt:Locator="Match(name)"
                                xdt:Transform="InsertIfMissing" />
            </environmentVariables>
        </aspNetCore>
        </system.webServer>
    </location>
    </configuration>
    
  • 使用特定名称创建项目概要文件,如
    Dev
    Release
    ,您可以使用现有概要文件名称创建web.{profile}.config

  • 发布后,它会将特定内容从
    web.{profile}.config
    复制到web.config

  • 当前,它将存在于publihshed文件夹中的
    web.config
    web.Dev.config
    web.Release.config
    ,如果您只喜欢
    web.config
    ,请更改
    Project.csproj
    ,如下所示,以排除未插入的文件

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
        <Configurations>Debug;Release;Dev</Configurations>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    </ItemGroup>
    <ItemGroup>
        <Content Update="web.Dev.config" CopyToPublishDirectory="Never" />
        <Content Update="web.Release.config" CopyToPublishDirectory="Never" />
    </ItemGroup>
    </Project>
    
    
    netcoreapp2.2
    过程中
    调试;释放;发展
    

  • 要使用特定环境发布
    web.config
    ,您可以尝试

    对于您的需求,您可以尝试
    Profile

  • 使用不同的
    ASPNETCORE\u环境变量创建多个web.{profile}.config

    例如,
    web.Dev.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
    <location>
        <system.webServer>
        <aspNetCore>
            <environmentVariables xdt:Transform="InsertIfMissing">
            <environmentVariable name="ASPNETCORE_ENVIRONMENT"
                                value="Dev"
                                xdt:Locator="Match(name)"
                                xdt:Transform="InsertIfMissing" />
            </environmentVariables>
        </aspNetCore>
        </system.webServer>
    </location>
    </configuration>
    
  • 使用特定名称创建项目概要文件,如
    Dev
    Release
    ,您可以使用现有概要文件名称创建web.{profile}.config

  • 发布后,它会将特定内容从
    web.{profile}.config
    复制到web.config

  • 当前,它将存在于publihshed文件夹中的
    web.config
    web.Dev.config
    web.Release.config
    ,如果您只喜欢
    web.config
    ,请更改
    Project.csproj
    ,如下所示,以排除未插入的文件

    <Project Sdk="Microsoft.NET.Sdk.Web">
    
    <PropertyGroup>
        <TargetFramework>netcoreapp2.2</TargetFramework>
        <AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
        <Configurations>Debug;Release;Dev</Configurations>
    </PropertyGroup>
    
    <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App" />
        <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.2.0" PrivateAssets="All" />
    </ItemGroup>
    <ItemGroup>
        <Content Update="web.Dev.config" CopyToPublishDirectory="Never" />
        <Content Update="web.Release.config" CopyToPublishDirectory="Never" />
    </ItemGroup>
    </Project>
    
    
    netcoreapp2.2
    过程中
    调试;释放;发展
    

  • 您只需在部分使用以下文本更新您的网络配置文件

    <configuration>
      <system.webServer>
        <aspNetCore .....>
          <environmentVariables>
            <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
          </environmentVariables>
        </aspNetCore>
      </system.webServer>
    </configuration>
    
    
    

    第二步:

    现在选择system.webserver/asp.netcore,并在其他下拉列表中选择applicationHost.config,如下图所示

    第三步:

    现在选择环境变量并输入值


    我希望它能对您有所帮助。

    您可以在部分用下面的文本更新您的网络配置文件

    <configuration>
      <system.webServer>
        <aspNetCore .....>
          <environmentVariables>
            <environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
          </environmentVariables>
        </aspNetCore>
      </system.webServer>
    </configuration>
    
    
    

    第二步:

    现在选择system.webserver/asp.netcore,并在其他下拉列表中选择applicationHost.config,如下图所示

    第三步:

    现在选择环境变量并输入值

    我希望它能帮助你。

    来自

    对于Windows IIS部署:在发布配置文件(.pubxml)或项目文件中包含属性。此方法在发布项目时在web.config中设置环境

    
    发展
    
    来自

    对于Windows IIS部署:在发布配置文件(.pubxml)或项目文件中包含属性。此方法在发布项目时在web.config中设置环境

    
    发展
    
    经过长时间的搜索、阅读和实验,我只需将环境变量
    ASPNETCORE\u ENVIRONMENT
    添加到我的
    .pubxml

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <ASPNETCORE_ENVIRONMENT>Test</ASPNETCORE_ENVIRONMENT>
      </PropertyGroup>
      <PropertyGroup>
        <WebPublishMethod>MSDeploy</WebPublishMethod>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        .....
      </PropertyGroup>
    </Project>
    
    最后,我的react应用程序的
    package.json

    "scripts": {
        "start": "env-cmd -f .env.development react-scripts start",
        "build:production": "env-cmd -f .env.production react-scripts build",
        "build:staging": "env-cmd -f .env.staging react-scripts build",
        "build:test": "env-cmd -f .env.test react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "mock:api": "json-server --watch db.json --port 4000"
      },
    
    我故意删除了
    “build”:“react scripts build”
    行,所以总是提供环境

    编辑:

    我还添加了
    PropertyGroup
    用于在
    .csproj
    文件中设置
    EnvironmentName
    ,以便在发布时,my
    web.config
    ASPNETCORE\u环境
    设置为环境变量,如my
    .pubxml
    中所定义,动态:

      <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:$(ASPNETCORE_ENVIRONMENT.ToLower())" />
        <PropertyGroup>
          <EnvironmentName>$(ASPNETCORE_ENVIRONMENT)</EnvironmentName>
        </PropertyGroup>
      </Target>
    
    
    $(ASPNETCORE_环境)
    
    经过长时间的搜索、阅读和实验,我只需将环境变量
    ASPNETCORE\u ENVIRONMENT
    添加到我的
    .pubxml

    <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
      <PropertyGroup>
        <ASPNETCORE_ENVIRONMENT>Test</ASPNETCORE_ENVIRONMENT>
      </PropertyGroup>
      <PropertyGroup>
        <WebPublishMethod>MSDeploy</WebPublishMethod>
        <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        .....
      </PropertyGroup>
    </Project>
    
    最后,我的react应用程序的
    package.json

    "scripts": {
        "start": "env-cmd -f .env.development react-scripts start",
        "build:production": "env-cmd -f .env.production react-scripts build",
        "build:staging": "env-cmd -f .env.staging react-scripts build",
        "build:test": "env-cmd -f .env.test react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject",
        "mock:api": "json-server --watch db.json --port 4000"
      },
    
    我故意删除了
    “build”:“react scripts build”
    行,所以总是提供环境

    编辑:

    我还添加了
    PropertyGroup
    用于在
    .csproj
    文件中设置
    EnvironmentName
    ,以便在发布时,my
    web.config
    ASPNETCORE\u环境
    设置为环境变量,如my
    .pubxml
    中所定义,动态:

      <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm install" />
        <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:$(ASPNETCORE_ENVIRONMENT.ToLower())" />
        <PropertyGroup>
          <EnvironmentName>$(ASPNETCORE_ENVIRONMENT)</EnvironmentName>
        </PropertyGroup>
      </Target>
    
    
    $(ASPNETCORE_环境)
    
    @yaakov的可能副本它显示了如何手动执行。我已经知道那部分了。我希望根据我使用的发布配置文件自动完成。如果您试图更改运行时行为并加载其他配置,则可以使用hostsettings.json文件并设置
    环境
    属性()文件,然后在发布管道中修改该文件。但如果您想在这之外执行此操作,我知道的唯一方法是在运行应用程序()之前指定它