Add custom domain names to web apps using REST API

2020-05-07 06:25发布

I am new person to Azure development. Is it possible to add custom domain names to web apps using .Net REST API?

2条回答
Summer. ? 凉城
2楼-- · 2020-05-07 06:42

https://docs.microsoft.com/en-us/azure/app-service/scripts/app-service-powershell-configure-custom-domain

Most easily done with 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")
查看更多
疯言疯语
3楼-- · 2020-05-07 06:59

yes, you can do that.

1) Install the NuGet Web Sites Management Package into your project.

2) Get Azure Publish Settings file (for example, by using Powershell Get-AzurePublishSettingsFile). You will need that later (the value of the management certificate field inside of that file).

2) Instantiate WebSiteManagementClient. That should help with the understanding of the code.

3) Next, the code is below. I just tested and it works. First, it lists the webspaces, then the websites inside of each of webspace, and you should copy and paste the website webspace into the

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();
    }
查看更多
登录 后发表回答