I am new person to Azure development. Is it possible to add custom domain names to web apps using .Net REST API?
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();
}