Asp.Net Azure 上的 VNext 应用程序设置

Asp.Net VNext App Settings on Azure

我非常喜欢 Asp.Net vNext 的新配置功能,默认使用 appsettings.json

但是我想在将网站发布为 Azure Web 应用程序时更改该文件的值。

旧的 web.config 应用程序设置很容易更改和配置环境属性。

你知道怎么做吗? 我更喜欢使用默认提供程序而不是创建自定义配置提供程序。

谢谢!

如果您在 Azure 门户中设置应用程序设置,它们将在运行时成为环境变量,并且应该由 ASP.NET vNext 运行时获取。因此,您无需身体上修改您的appsettings.json即可实现此目的。

大卫,效果很好!谢谢!

这里有一个示例可以帮助我们的朋友解决同样的问题:

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

要查看差异,运行 本地主机上的网络应用程序和 Azure 网络应用程序更改应用程序设置 Data:DefaultConnection:ConnectionString

最佳,