我非常喜欢使用de default appsettings.json的Asp.Net vNext的新配置功能
但是当我将网站发布为Azure Web App时,我想更改该文件的值。
旧的web.config appsettings很容易更改并配置环境中的属性。
你知道怎么做吗? 我更喜欢使用默认提供程序,而不是创建自定义配置提供程序。
谢谢!
答案 0 :(得分:6)
如果在Azure门户中设置应用程序设置,它们将在运行时成为环境变量,并且应该被ASP.NET vNext运行时选中。因此,您无需物理修改appsettings.json
即可实现此目的。
答案 1 :(得分:1)
It worked very well David! Thank you!
Here a sample to help our friends with the same question:
startup.cs
public Startup(IHostingEnvironment env)
{
// Set up configuration sources.
var builder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
if (env.IsDevelopment())
{
// For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
builder.AddUserSecrets();
}
**builder.AddEnvironmentVariables();**
Configuration = builder.Build();
}
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
// Add application services.
services.AddTransient<IEmailSender, AuthMessageSender>();
services.AddTransient<ISmsSender, AuthMessageSender>();
**services.AddInstance<IConfiguration>(Configuration);**
}
HomeController.cs
IConfiguration _configuration;
public HomeController(IConfiguration configuration)
{
this._configuration = configuration;
}
public IActionResult Index()
{
ViewBag.key = _configuration["Data:DefaultConnection:ConnectionString"];
return View();
}
Index.cshtml
@{
ViewData["Title"] = "Home Page";
} @ViewBag.key
To see the difference, run the web app on localhost and on an azure web app changing the appsetting Data:DefaultConnection:ConnectionString
Best,