Dynamically create a website on azure
This article describes how to dynamically create a website on the deployed webrole. Of course, you can also use the role deployment package to create multiple websites or web virtual directories together, for this method, see my other article "Deploying webapps and WebServices in a webrole at the same time on windowsazure".
1. Upgrade webrole to administrator.
Append the following configuration in the servicedefinition. csdef file.
<WebRole name="MvcWebRole1" vmsize="Small"> <Runtime executionContext="elevated"></Runtime> </WebRole>
2,Port shielding on the firewall.
When we use conventional methods to deploy the site, our applications either use the same port with different hostnames or use the same IP address on different ports. On Azure, because azure only opens a fixed port in the firewall when deploying the file package (except for the port of Remote Desktop Connection, it is difficult to use different ports to create a website dynamically by using the same port and different hostnames. You can view which port is open on the Windows azure Management Portal.
3,Microsoft. Web. Administration assembly
In the code for creating a website, you must reference the Microsoft. Web. Administration assembly in the % WINDIR % \ system32 \ inetsrv directory. Because the GAC of azurehost does not contain the Assembly by default, you need to set the copy local attribute of the assembly to true.
4,Dynamic website creation code
String newwebsitepath = "your app path"; using (servermanager = new servermanager () {/* create application pool */applicationpool = servermanager. applicationpools. add ("myapplicationpool"); applicationpool. autostart = true; applicationpool. managedpipelinemode = managedpipelinemode. integrated; applicationpool. managedruntimeversion = "v4.0";/* create a web site */site = servermanager. sites. add ("mynewsite", "HTTP", "*: 80: www.mysite.com", newwebsitepath); site. applications [0]. applicationpoolname = "myapplicationpool"; site. serverautostart = true; servermanager. commitchanges ();}
5,Test the dynamically created website
This is also very simple. First, find the IP address of your deployed webrole in the window azure Management Portal, and then open the c: \ windows \ system32 \ drivers \ etc \ hosts file, add a record in the following format.
111.222.333.444 www.mysite.com
Then, enter www.mysite.com in your browser to see if you have created the site dynamically.