【发布时间】:2020-04-20 20:18:56
【问题描述】:
我有点困惑如何处理 Web 项目中的本地设置,对于我们的 Azure 功能,这似乎非常简单,我们使用 local.settings.json 然后我们使用 ConfigurationBuilder 如下处理本地与 Azure 配置设置
private static readonly IConfiguration config = new ConfigurationBuilder()
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();
private static readonly string cosmosDBEndpointUrl = config["cosmosDBEndpointUrl"];
private static readonly string cosmosDBPrimaryKey = config["cosmosDBPrimaryKey"];
private static readonly string cosmosDBName = config["cosmosDBName"];
private static readonly string cosmosDBCollectionNameRawData = config["cosmosDBCollectionNameRawData"];
这适用于我们的函数,因此它可以读取本地 dev 的本地设置和我们发布到的任何 env 的 azure 设置。
但是在我的 webproject 中没有 local.settings.json 那么我该如何处理那里的相同场景呢?
我查看了 appsettings.json,但格式似乎不同。
【问题讨论】:
-
据我所知,在 ASP.NET Core Web 应用中,我们可以将应用设置保存在 appsettings.json 中(如
{ "MyConfig": { "ApplicationName": "MyApp", "Version": "1.0.0" } })。关于如何阅读,请参考stackoverflow.com/questions/31453495/… -
在 .Net Framework web 应用中,我们可以在 web.config 中使用
元素为 保存应用设置(例如 <?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key="countoffiles" value="7" /> <add key="logfilelocation" value="abc.txt" /> </appSettings> </configuration>)。关于如何阅读,我们可以使用System.Configuration.ConfigurationManager.AppSettings["<key name>"];
标签: c# azure-functions azure-web-app-service