可以通过构建目标访问dotnet的环境变量
我正在尝试访问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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
启动settings.json
运行应用程序时使用。从那里的值在编译时间内不可用。这就是为什么在尝试读取值时会得到一个空字符串的原因。您可以做的是根据可用喜欢
configuration
。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
.