【发布时间】:2014-05-26 16:39:49
【问题描述】:
是否可以从应用程序本身更改网站的应用程序设置?
这不是日常操作,而是自助式重新配置选项。非开发人员可以更改特定设置,这应该会导致重启,就像我可以在网站配置页面(应用设置部分)上手动执行的操作一样
【问题讨论】:
标签: azure azure-web-app-service appsettings
是否可以从应用程序本身更改网站的应用程序设置?
这不是日常操作,而是自助式重新配置选项。非开发人员可以更改特定设置,这应该会导致重启,就像我可以在网站配置页面(应用设置部分)上手动执行的操作一样
【问题讨论】:
标签: azure azure-web-app-service appsettings
您还可以使用 Azure Fluent Api。
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
...
public void UpdateSetting(string key, string value)
{
string tenantId = "a5fd91ad-....-....-....-............";
string clientSecret = "8a9mSPas....................................=";
string clientId = "3030efa6-....-....-....-............";
string subscriptionId = "a4a5aff6-....-....-....-............";
var azureCredentials = new AzureCredentials(new
ServicePrincipalLoginInformation
{
ClientId = clientId,
ClientSecret = clientSecret
}, tenantId, AzureEnvironment.AzureGlobalCloud);
var _azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(azureCredentials)
.WithSubscription(subscriptionId);
var appResourceId = "/subscriptions/xxx/resourceGroups/xxx/providers/Microsoft.Web/sites/xxx"; //Get From WebApp -> Properties -> Resource ID
var webapp = _azure.WebApps.GetById(appResourceId);
//Set App Setting Key and Value
webapp.Update()
.WithAppSetting(key, value)
.Apply();
}
【讨论】:
var _azure = Azure.Configure()... Azure 找不到编译时错误,我需要安装任何其他nuget 包来解决它吗?
一旦我找到了合适的库来做到这一点,这并不难,Microsoft Azure Web Sites Management Library。
var credentials = GetCredentials(/*using certificate*/);
using (var client = new WebSiteManagementClient(credentials))
{
var currentConfig = await client.WebSites.GetConfigurationAsync(webSpaceName,
webSiteName);
var newConfig = new WebSiteUpdateConfigurationParameters
{
ConnectionStrings = null,
DefaultDocuments = null,
HandlerMappings = null,
Metadata = null,
AppSettings = currentConfig.AppSettings
};
newConfig.AppSettings[mySetting] = newValue;
await client.WebSites.UpdateConfigurationAsync(webSpaceName, webSiteName,
newConfig);
}
【讨论】:
webSpaceName 和 webSiteName 的位置?
你读过Service Management REST API吗?文档提到它允许您以编程方式执行通过管理门户可用的大多数操作。
【讨论】:
除了 Diego 的回答,要在 WebApp(网站和/或 WebJobs)中使用 Azure 管理库,您需要配置 SSL,这有点棘手:
【讨论】: