Asp.Net VNext App Settings on Azure

ぐ巨炮叔叔 提交于 2019-12-19 10:16:37

问题


I really enjoyed the new Configuration feature of Asp.Net vNext using de default appsettings.json

But I would like to change the values of that file when I publish the website as a Azure Web App.

The old web.config appsettings was easy to change and configure the properties on the environment.

Do you know how to do this? I prefer to user the default provider instead of creating a custom configuration provider.

Thank you!


回答1:


If you set App Settings in the Azure Portal, they become environment variables at runtime, and should get picked up by the ASP.NET vNext runtime. So you don't need to physically modify your appsettings.json to achieve this.




回答2:


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,



来源:https://stackoverflow.com/questions/34732620/asp-net-vnext-app-settings-on-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!