Add new Virtual Directories to an Azure WebApp programmatically

196 views Asked by At

Is it possible to add a Virtual Directory to an Azure Web Apps Website using Azure SDK or Azure REST API?

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 (e.g. www.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. 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.

Please help.

1

There are 1 answers

0
Joey Cai On

You can use management rest api to create a virtual directory to 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));
    }