Last active
August 29, 2015 14:21
-
-
Save yetanotherchris/7e14419d3f767de37db7 to your computer and use it in GitHub Desktop.
A small class for getting site information out of an ApplicationHost.config (the idea being you don't have to copy this applicationhost over the existing one)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ApplicationHostParser | |
{ | |
public void GetSites() | |
{ | |
string xml = File.ReadAllText("applicationHost.config"); | |
XDocument doc = XDocument.Load(new StringReader(xml)); | |
IEnumerable<XElement> siteNode = doc.Document.Descendants().FirstOrDefault(x => x.Name.LocalName == "system.applicationHost").Descendants().Where(x => x.Name.LocalName == "sites"); | |
IEnumerable<XElement> sites = siteNode.Elements().Where(x => x.Name.LocalName == "site"); | |
Console.WriteLine("Found {0} sites", sites.Count()); | |
List<Site> list = new List<Site>(); | |
foreach (XElement siteElement in sites) | |
{ | |
Site site = new Site() | |
{ | |
Name = siteElement.Attribute("name").Value | |
}; | |
SetPorts(siteElement, site); | |
site.SiteApplications = GetApplications(siteElement); | |
Console.WriteLine(site); | |
foreach (SiteApplication application in site.SiteApplications) | |
{ | |
Console.WriteLine(" {0}", application); | |
} | |
} | |
} | |
private void SetPorts(XElement siteElement, Site site) | |
{ | |
IEnumerable<XElement> bindings = siteElement.Elements().Where(x => x.Name.LocalName == "bindings") | |
.Descendants().Where(x => x.Name.LocalName == "binding"); | |
var httpBinding = bindings.FirstOrDefault(x => x.Attribute("protocol").Value == "http"); | |
var httpsBinding = bindings.FirstOrDefault(x => x.Attribute("protocol").Value == "https"); | |
if (httpBinding != null) | |
{ | |
site.Port = BindingStringToNumber(httpBinding.Attribute("bindingInformation").Value); | |
} | |
if (httpsBinding != null) | |
{ | |
site.SecurePort = BindingStringToNumber(httpsBinding.Attribute("bindingInformation").Value); | |
} | |
} | |
private int BindingStringToNumber(string bindingInformation) | |
{ | |
bindingInformation = bindingInformation.Replace(":", "").Replace("*", ""); | |
return int.Parse(bindingInformation); | |
} | |
private List<SiteApplication> GetApplications(XElement siteElement) | |
{ | |
List<SiteApplication> siteApplications = new List<SiteApplication>(); | |
IEnumerable<XElement> applicationElements = siteElement.Elements().Where(x => x.Name.LocalName == "application"); | |
foreach (XElement app in applicationElements) | |
{ | |
var siteApplication = new SiteApplication() | |
{ | |
ApplicationPool = (app.Attribute("applicationPool") != null) ? app.Attribute("applicationPool").Value : "", | |
Path = app.Attribute("path").Value | |
}; | |
siteApplication.VirtualDirectories = GetVirtualDirectories(app); | |
if (!string.IsNullOrEmpty(siteApplication.PhysicalPath)) | |
{ | |
siteApplications.Add(siteApplication); | |
} | |
else | |
{ | |
Console.WriteLine("** Skipping {0} it has no physical path defined**", siteApplication.Path); | |
} | |
} | |
return siteApplications; | |
} | |
private List<VirtualDirectory> GetVirtualDirectories(XElement applicationeElement) | |
{ | |
List<VirtualDirectory> virtualDirectories = new List<VirtualDirectory>(); | |
IEnumerable<XElement> directoryElements = applicationeElement.Elements().Where(x => x.Name.LocalName == "virtualDirectory"); | |
foreach (XElement app in directoryElements) | |
{ | |
var virtualDirectory = new VirtualDirectory() | |
{ | |
Path = app.Attribute("path").Value, | |
PhysicalPath = app.Attribute("physicalPath").Value | |
}; | |
virtualDirectories.Add(virtualDirectory); | |
} | |
return virtualDirectories; | |
} | |
} | |
public class Site | |
{ | |
public string Name { get; set; } | |
public int Port { get; set; } | |
public int SecurePort { get; set; } | |
public string WebRoot | |
{ | |
get { return ParseWebRoot(); } | |
} | |
public string ApplicationPool | |
{ | |
get { return ParseApplicationPool(); } | |
} | |
public List<SiteApplication> SiteApplications { get; set; } | |
public Site() | |
{ | |
SiteApplications = new List<SiteApplication>(); | |
} | |
private string ParseWebRoot() | |
{ | |
return SiteApplications.FirstOrDefault(x => x.Path == "/") | |
.VirtualDirectories.FirstOrDefault(x => x.Path == "/").PhysicalPath; | |
} | |
private string ParseApplicationPool() | |
{ | |
return SiteApplications.FirstOrDefault(x => x.Path == "/").ApplicationPool; | |
} | |
public override string ToString() | |
{ | |
return string.Format("[{0}] ({1}) - {2} / {3}", Name, WebRoot, Port, SecurePort); | |
} | |
} | |
public class SiteApplication | |
{ | |
public string Path { get; set; } | |
public string ApplicationPool { get; set; } | |
public string PhysicalPath | |
{ | |
get { return ParsePhysicalPath(); } | |
} | |
public List<VirtualDirectory> VirtualDirectories { get; set; } | |
public SiteApplication() | |
{ | |
VirtualDirectories = new List<VirtualDirectory>(); | |
} | |
private string ParsePhysicalPath() | |
{ | |
// Invalid entry | |
if (VirtualDirectories.Count == 0) | |
return ""; | |
return VirtualDirectories.FirstOrDefault(x => x.Path == "/").PhysicalPath; | |
} | |
public override string ToString() | |
{ | |
return string.Format("[{0}] ({1})", Path, PhysicalPath); | |
} | |
} | |
public class VirtualDirectory | |
{ | |
public string Path { get; set; } | |
public string PhysicalPath { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment