【发布时间】:2016-04-25 11:57:28
【问题描述】:
我是 Azure 开发的新手。是否可以使用 .Net REST API 向 Web 应用程序添加自定义域名?
【问题讨论】:
-
您好。您是否会考虑回复下面您帮助过您的人,和/或勾选他们的答案以将其标记为正确?
-
没有回复,所以投票结束。
标签: azure azure-web-app-service azure-api-apps
我是 Azure 开发的新手。是否可以使用 .Net REST API 向 Web 应用程序添加自定义域名?
【问题讨论】:
标签: azure azure-web-app-service azure-api-apps
是的,你可以这样做。
1) 将 NuGet Web Sites Management Package 安装到您的项目中。
2) 获取 Azure 发布设置文件(例如,通过使用 Powershell Get-AzurePublishSettingsFile)。稍后您将需要它(该文件中管理证书字段的值)。
2) 实例化 WebSiteManagementClient。 That 应该有助于理解代码。
3) 接下来,代码如下。我刚刚测试过,它可以工作。首先,它列出了网站空间,然后是每个网站空间内的网站,您应该将网站网站空间复制并粘贴到
public const string base64EncodedCertificate = "ManagementCertificateValueFromPublishSettingsFile";
public const string subscriptionId = "AzureSubscriptionId";
static SubscriptionCloudCredentials getCredentials()
{
return new CertificateCloudCredentials(subscriptionId, new X509Certificate2(Convert.FromBase64String(base64EncodedCertificate)));
}
static void Main(string[] args)
{
WebSiteManagementClient client = new WebSiteManagementClient(getCredentials());
WebSpacesListResponse n = client.WebSpaces.List();
n.Select(p =>
{
Console.WriteLine("webspace {0}", p.Name);
WebSpacesListWebSitesResponse websitesInWebspace = client.WebSpaces.ListWebSites(p.Name,
new WebSiteListParameters()
{
});
websitesInWebspace.Select(o =>
{
Console.Write(o.Name);
return o;
}).ToArray();
return p;
}).ToArray();
Console.ReadLine();
var configuration = client.WebSites.Get("WebSpaceName", "WebSiteName", new WebSiteGetParameters());
configuration.WebSite.HostNames.Add("new domain");
var resp = client.WebSites.Update("WebSpaceName", "WebSiteName", new WebSiteUpdateParameters() { HostNames = configuration.WebSite.HostNames });
Console.WriteLine(resp.StatusCode);
Console.ReadLine();
}
【讨论】:
使用 PowerShell 最容易完成。
$fqdn="<Replace with your custom domain name>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"
# Create a resource group.
New-AzureRmResourceGroup -Name $webappname -Location $location
# Create an App Service plan in Free tier.
New-AzureRmAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName $webappname -Tier Free
# Create a web app.
New-AzureRmWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName $webappname
Write-Host "Configure a CNAME record that maps $fqdn to $webappname.azurewebsites.net"
Read-Host "Press [Enter] key when ready ..."
# Before continuing, go to your DNS configuration UI for your custom domain and follow the
# instructions at https://aka.ms/appservicecustomdns to configure a CNAME record for the
# hostname "www" and point it your web app's default domain name.
# Upgrade App Service plan to Shared tier (minimum required by custom domains)
Set-AzureRmAppServicePlan -Name $webappname -ResourceGroupName $webappname `
-Tier Shared
# Add a custom domain name to the web app.
Set-AzureRmWebApp -Name $webappname -ResourceGroupName $webappname `
-HostNames @($fqdn,"$webappname.azurewebsites.net")
【讨论】: