'The server manager returns null

I am trying to launch my application and getting the below error. Here is my configuration: Ports Number changed, Module paths are added in applicationhost.config file

using (ServerManager serverManager = new ServerManager())
{
    var modules = (from key in System.Configuration.ConfigurationManager.AppSettings.AllKeys
                   where key.EndsWith("ModulePath")
                   select new
                   {
                       path = "/" + key.Substring(0, key.IndexOf("ModulePath")),
                       physical_path = System.Configuration.ConfigurationManager.AppSettings[key]
                   }).ToList();

    Microsoft.Web.Administration.Configuration config = serverManager.GetApplicationHostConfiguration();
    ConfigurationSection sitesSection = config.GetSection("system.applicationHost/sites");
    ConfigurationElementCollection sitesCollection = sitesSection.GetCollection();

    var siteName = System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName();
    var site = serverManager.Sites.FirstOrDefault(c => c.Name == siteName);

    ConfigurationElement siteElement = FindElement(sitesCollection, "site", "name", siteName);
    ConfigurationElementCollection siteCollection = siteElement.GetCollection();
    ConfigurationElement applicationElement = FindElement(siteCollection, "application", "path", @"/");
}

It says unable to add virtual directories for module path.

I have recently enabled the IIS.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source