可以通过构建目标访问dotnet的环境变量

发布于 2025-02-12 00:49:56 字数 1284 浏览 2 评论 0原文

我正在尝试访问ASPNETCORE_ENVIRONMENT环境变量target's 构建事件。

.csproj看起来像这样:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>

    <Target Name="MyPostBuild" BeforeTargets="Build">
        <Exec Command="echo Operating System: $(OS)"/>
        <Exec Command="echo ASPNETCORE_ENVIRONMENT: $(ASPNETCORE_ENVIRONMENT)"/>
    </Target>

</Project>

启动stettings.json包含环境变量:

"profiles": {
    "DemoProject": {
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
} 

它输出$(OS)很好,但它不会打印aspnetcore_environment /代码>:

操作系统Windows_nt

aspnetcore_environment:

我的问题是如何访问aspnetcore_environment build> build> build> build event event 以使构建事件仅在特定条件下运行,这:

<Target Name="MyPostBuild" BeforeTargets="Build" Condition="'$(ASPNETCORE_ENVIRONMENT)' == 'Development'">
    Do something
</Target>

I'm trying to access the ASPNETCORE_ENVIRONMENT environment variable inside a Target's Build event.

The .csproj looks like this:

<Project Sdk="Microsoft.NET.Sdk.Web">

    <PropertyGroup>
        <TargetFramework>net6.0</TargetFramework>
    </PropertyGroup>

    <Target Name="MyPostBuild" BeforeTargets="Build">
        <Exec Command="echo Operating System: $(OS)"/>
        <Exec Command="echo ASPNETCORE_ENVIRONMENT: $(ASPNETCORE_ENVIRONMENT)"/>
    </Target>

</Project>

The launchSettings.json contain the environment variable:

"profiles": {
    "DemoProject": {
        "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
        }
    }
} 

It outputs the $(OS) very well but it does not prints the ASPNETCORE_ENVIRONMENT:

Operating System Windows_NT

ASPNETCORE_ENVIRONMENT:

My question is how to access the ASPNETCORE_ENVIRONMENT value within a Build event in order to make the build event run only with a specific condition like this:

<Target Name="MyPostBuild" BeforeTargets="Build" Condition="'$(ASPNETCORE_ENVIRONMENT)' == 'Development'">
    Do something
</Target>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

咆哮 2025-02-19 00:49:57

启动settings.json运行应用程序时使用。从那里的值在编译时间内不可用。这就是为什么在尝试读取值时会得到一个空字符串的原因。

您可以做的是根据可用喜欢configuration

'$(Configuration)' == 'Debug'

launchSettings.json is used when you run your application. The values from there are not available in compile time. That's why you get an empty string when trying to read the value.

What you could do instead is to do a condition based on one of the values that are available like Configuration.

'$(Configuration)' == 'Debug'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文