We referred stackOverflow and a few other sites to create a website on IIS using console application. It worked after we run the console with administration permission. Code does create an Application pool and host site on a given port. Same code when we tried on asp.net web application it completed its execution but we did not found metadata on IIS.
Here is a code that works on console..
try
{
ServerManager server = new ServerManager();
ApplicationPool myApplicationPool = null;
//we will first check to make sure that this pool does not already exist
//since the ApplicationPools property is a collection, we can use the Linq FirstOrDefault method
//to check for its existence by name
if (server.ApplicationPools != null && server.ApplicationPools.Count > 0)
{
if (server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool") == null)
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
else
{
//if we find the pool already there, we will get a referecne to it for update
myApplicationPool = server.ApplicationPools.FirstOrDefault(p => p.Name == "TestPool");
}
}
else
{
//if the pool is not already there we will create it
myApplicationPool = server.ApplicationPools.Add("TestPool");
}
if (myApplicationPool != null)
{
//for this sample, we will set the pool to run under the NetworkService identity
myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.NetworkService;
//for this sample, we will set the pool to run under the identity of a specific user
//myApplicationPool.ProcessModel.IdentityType =
ProcessModelIdentityType.SpecificUser;
//myApplicationPool.ProcessModel.UserName = UserName;
//myApplicationPool.ProcessModel.Password = Password;
//we set the runtime version
myApplicationPool.ManagedRuntimeVersion = "v4.0";
//we save our new ApplicationPool!
server.CommitChanges();
}
//Create website
if (server.Sites != null && server.Sites.Count > 0)
{
//we will first check to make sure that the site isn't already there
if (server.Sites.FirstOrDefault(s => s.Name == "MySite") == null)
{
//we will just pick an arbitrary location for the site
string path = @"C:\inetpub\Custom";
//we must specify the Binding information
string ip = "*";
string port = "98";
string hostName = "*";
string bindingInfo = string.Format(@"{0}:{1}:{2}", ip, port, hostName);
//add the new Site to the Sites collection
Site site = server.Sites.Add("MySite", "http", bindingInfo, path);
//set the ApplicationPool for the new Site
site.ApplicationDefaults.ApplicationPoolName = myApplicationPool.Name;
//save the new Site!
server.CommitChanges();
Console.WriteLine("Web site created successfully...");
Console.ReadLine();
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadLine()
}
Here site gets listed in IIS as well.
Now when tried the same code on the web application, it does not create anything on IIS. when we inspect the server manager object we found that the Application pool list was coming from the project's
applicationhost.config
file which is located on .vs hidden folder.
We installed the latest IIS on the local machine for a test, is there any changes needed to get it to work on the web as well. (: we are new to IIS stuff)