我正在尝试在测试API上加载我的appsettings.test.json文件,但阅读环境变量时遇到问题。在本地运行正常,但当我将其推送到Azure应用服务时,总是指向我的dev appsettings.json文件。
我将在下面附上我的设置和我用来验证这个问题的API调用结果。每个其他自定义值都可以在ASPNETCORE_ENVIRONMENT之外使用。
Azure应用服务应用程序设置
用于测试的应用程序服务终结点
[HttpGet("env")]
public IActionResult GetEnvVariable()
{
var test = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
var test1 = Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT");
return Ok($"ASPNETCORE: {test}\nDOTNET: {test1}");
}
应用程序服务终结点响应
程序. cs
public class Program
{
public static void Main(string[] args)
{
CreateWebHostBuilder(args).Build().Run();
}
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
WebHost.CreateDefaultBuilder(args).UseStartup<Startup>();
}
程序. cs
public class Startup
{
public Startup(IConfiguration configuration, IWebHostEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();
Configuration = builder.Build();
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
//register bugsnag, swagger, jwt auth, auto mapper, service classes, db context, controllers
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseSwagger();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwaggerUI(options =>
{
options.SwaggerEndpoint("/swagger/v1/swagger.json", "Test API V1");
});
}
app.UseAuthentication();
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseMiddleware<GlobalErrorHandlingMiddleware>();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}
CSPROJ
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>$(MSBuildProjectName)</AssemblyName>
<RootNamespace>$(MSBuildProjectName.Replace(" ", "_"))</RootNamespace>
<Configurations>Release;Debug;Test</Configurations>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<Optimize>True</Optimize>
<DefineConstants>$(DefineConstants)</DefineConstants>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Test|AnyCPU' ">
<Optimize>True</Optimize>
<DefineConstants>$(DefineConstants)</DefineConstants>
<IntermediateOutputPath>obj\Release\net6.0</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>$(DefineConstants)</DefineConstants>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Content Remove="appsettings.Development.json" />
<Content Remove="appsettings.json" />
<Content Remove="appsettings.Production.json" />
<Content Remove="appsettings.Test.json" />
</ItemGroup>
<ItemGroup>
<None Include="appsettings.Development.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Include="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Include="appsettings.Production.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
<None Include="appsettings.Test.json">
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.1" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Serilog.AspNetCore" Version="6.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Test.Api.Business\Test.Api.Business.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="wwwroot\" />
</ItemGroup>
</Project>
其他信息此应用程序服务使用F1免费套餐,这可能是原因吗?
1条答案
按热度按时间ffvjumwh1#
如果有人遇到这个问题,我的项目最初是一个.NET CORE 3.1项目,即使升级到6后,显然仍然有挥之不去的文件,不会被清除,只是因为我针对一个较新的框架。
罪魁祸首是我的
web.config
文件,它覆盖了我的ASPNETCORE_ENVIRONMENT
env变量。