我是Azure开发的新人。是否可以使用.Net REST API向Web应用程序添加自定义域名?
答案 0 :(得分:2)
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();
}
答案 1 :(得分:0)
使用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")