Asp.Net VNext App Settings on Azure

淺唱寂寞╮ 提交于 2019-12-01 12:06:40

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.

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,

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