我已将 Azure 添加到项目中,但现在无法在项目构建中找到 appsettings.json 文件

I have added Azure to project and now cannot find appsettings.json file on build of project

我收到一个错误

System.IO.InvalidDataException: 'Failed to load configuration from file 'C:\Users\jonat\WorkFolder\newCapstone\TheSatoshiEra\TheSatoshiEra\BackendSetup\TheSatoshiEra\TheSatoshiEra\appsettings.json'.

My code for my program file is

var builder = WebApplication.CreateBuilder(args); 

// Add services to the container.

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
  app.UseSwagger();
  app.UseSwaggerUI();
}

app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run(); 

我的 appsettings.json 的代码是

{   "Logging": 
 {     "LogLevel":
  {     
   "Default": "Information",      
   "Microsoft.AspNetCore": "Warning"    
  }  
 },  
 "ConnectionStrings":
 {   
  "TheSatoshiEra": "Server=localhost;Database=TheSatoshiEra;Trusted_Connection=True;",     "AllowedHosts": "*"  
 }

Lastly the code for my Startup.cs file is

using Microsoft.AspNetCore.Builder; ``
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;`
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TheSatoshiEra.DataAccess;

namespace TheSatoshiEra
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddSingleton(Configuration);
    
            services.AddTransient<UsersRepository>();
            services.AddTransient<AboutRepository>();
            services.AddTransient<UserTypesRepository>();
            services.AddTransient<LibraryRepository>();
    
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "TheSatoshiEra", Version = "v1" });
            });
        }
    
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "TheSatoshiEra v1"));
            }
    
            app.UseHttpsRedirection();
    
            app.UseRouting();
    
            app.UseCors(cfg => cfg.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin());
    
    
            app.UseAuthorization();
    
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

}

我尝试在我的 IIS express 文件夹中添加诸如 Web 部署之类的包我还重新配置了连接字符串,并在 azure 中检查了我是否能够使用 sql 命令回调数据。我在项目中添加了 cors 并尝试将 appsettingsfile 重新配置为 "Data": { "ConnectionStrings":{

  • 应用服务值允许您使用生产设置 运行 生产中的应用,而本地 appsettings.json 值允许您在本地调试应用。
  • 通过应用程序设置菜单项配置您的 Azure Web 应用程序, appsettings.json 或其他 config files 被忽略。
  • 确保 "PropertyGroup" 部分具有以下 "appsettings.json" ItemGroup
  • 的设置
 <ItemGroup>
    <None Update="appsettings.json">
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </None>
  </ItemGroup>

  • 通过添加以上行我可以看到 appsettings.json 正在部署到 KUDU 控制台中的 wwwroot 文件夹

  • 您的 appsettings.json 文件不需要包含在发布选项中。

  • 在添加 json 文件之前设置基本路径。

 public Startup(IHostingEnvironment environment)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(environment.ContentRootPath)
            .AddJsonFile("appsettings.json");

        Configuration = builder.Build();
    }
  • HostingEnvironment用于构造启动函数Object(){[native code]},base路径设置为当前根路径

  • 要定义您自己的/更改环境变量,请转至 Azure 门户 → 您的应用服务 → 配置 → 应用程序设置:添加新的应用程序/连接设置

更多信息请参考Configure apps - Azure App Service and SO Threads S1 and