简体   繁体   English

以编程方式向 Azure WebApp 添加新的虚拟目录

[英]Add new Virtual Directories to an Azure WebApp programmatically

Is it possible to add a Virtual Directory to an Azure Web Apps Website using Azure SDK or Azure REST API?是否可以使用 Azure SDK 或 Azure REST API 将虚拟目录添加到 Azure Web Apps 网站?

We have a ASP.NET CMS website (the main site is at www.example.com ) which has the ability to create new website instances pointing to the same source code as the main site with a level 1 virtual directory name (eg www.example.com/project1 ).我们有一个 ASP.NET CMS 网站(主站点位于www.example.com ),它能够创建指向与主站点相同的源代码的新网站实例,并使用 1 级虚拟目录名称(例如www.example.com。 example.com/project1 )。 Each virtual directory will have its own users and administration.每个虚拟目录都有自己的用户和管理。

I'm able to add a virtual directory manually in the Azure portal.我可以在 Azure 门户中手动添加虚拟目录。 I'm also able to use .NET API to create it programmatically in my local IIS ( ServerManager from Microsoft.Web.Administration ), but it doesn't work in Azure Web Apps.我还可以使用 .NET API 在我的本地 IIS(来自Microsoft.Web.Administration 的ServerManager )中以编程方式创建它,但它在 Azure Web Apps 中不起作用。

Please help.请帮忙。

You can use management rest api to create a virtual directory to azure webapp.您可以使用管理 rest api 为 azure webapp 创建一个虚拟目录。

public void CreateVD(string name)
{
    try
    {
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}/config/web?api-version=2015-08-01",subscriptionId,webSpaceName,webSiteName));
        request.Headers.Add("x-ms-version", "2013-03-01");
        request.ContentType = "application/json";
        var token = GetAuthorizationHeader();
        request.Headers.Add("Authorization", "Bearer" + " " + token);

        System.Net.WebResponse response = request.GetResponse();

        string data = "";
        using (System.IO.Stream stream = response.GetResponseStream())
        {
              System.IO.StreamReader sr = new System.IO.StreamReader(stream);
              data = sr.ReadToEnd();
              sr.Close();
              stream.Close();
              Console.WriteLine("data found");
        }

        if (data == "")
        {
              Console.WriteLine("Error in collecting data");
                    return;
        }

        string path = name, directory = name;
        data = data.Replace("virtualApplications\":[", "virtualApplications\":[{\"virtualPath\":\"/" + path + "\",\"physicalPath\":\"site\\\\wwwroot\\\\" + directory + "\",\"preloadEnabled\":false,\"virtualDirectories\":null},");
        request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(String.Format(@"https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Web/sites/{2}/config/web?api-version=2015-08-01",subscriptionId,webSpaceName,webSiteName));
        request.Headers.Add("x-ms-version", "2013-03-01");
        request.ContentType = "application/json";
        request.AllowWriteStreamBuffering = false;
        request.Accept = "Accept=application/json";
        request.SendChunked = false;
        request.Headers.Add("Authorization", "Bearer" + " " + token);
        request.ContentLength = data.Length;
        request.Method = "PUT";

        System.IO.StreamWriter sw = new System.IO.StreamWriter(request.GetRequestStream());
        sw.Write(data);
        sw.Close();

        response = request.GetResponse();

        using (System.IO.Stream stream = response.GetResponseStream())
        {
             data = (new System.IO.StreamReader(stream)).ReadToEnd();
             Console.WriteLine("DONE");
        }
   }
   catch (Exception ex)
   {
        Console.WriteLine(ex.StackTrace);
   }
}

For the access token, you could use following code to get the token.对于访问令牌,您可以使用以下代码来获取令牌。

private string GetAuthorizationHeader()
{
    AuthenticationResult result = null;

    var context = new AuthenticationContext(string.Format(loginpath, tenantId));

    result = context.AcquireToken(apiEndpoint, clientId, new Uri(redirectUri));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM