使用 REST API 将自定义域名添加到 Web 应用程序 [英] Add custom domain names to web apps using REST API

查看:18
本文介绍了使用 REST API 将自定义域名添加到 Web 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Azure 开发的新手.是否可以使用 .Net REST API 向 Web 应用程序添加自定义域名?

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

推荐答案

是的,你可以这样做.

1) 将 NuGet 网站管理包 安装到您的项目.

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

2) 获取 Azure 发布设置文件(例如,通过使用 Powershell Get-AzurePublishSettingsFile).稍后您将需要它(该文件中管理证书字段的值).

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) 实例化 WebSiteManagementClient.那个 应该有助于理解代码.

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

3) 接下来,代码如下.我刚刚测试过,它可以工作.首先,它列出了网站空间,然后是每个网站空间内的网站,您应该将网站网站空间复制并粘贴到

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

这篇关于使用 REST API 将自定义域名添加到 Web 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆