Add custom domain names to web apps using REST API

牧云@^-^@ 提交于 2019-12-31 05:25:26

问题


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


回答1:


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();
    }



回答2:


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")


来源:https://stackoverflow.com/questions/36839883/add-custom-domain-names-to-web-apps-using-rest-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!