How to get "Browse" URL for web site in IIS using C#?
Solution 1
Right click and go to edit bindings...
under Host Name
you can actually see which domain it is.
Or
Click the site and on actions tab on right hand side you can click bindings...
To Get the URL :
HttpContext.Current.Request.Url.AbsoluteUri;
//http://localhost:8080/app1/Default.aspx
HttpContext.Current.Request.Url.AbsolutePath;
// /YourSite/app1/Defaul.aspx
HttpContext.Current.Request.Url.Host;
// localhost:8080
Edit:
To get site information try using HostingEnvironment.ApplicationHost.GetSiteName()
or HostingEnvironment.ApplicationHost.GetSiteID()
see below sample(it is not tested) :
using (ServerManager sm = new ServerManager())
{
foreach (Binding b in sm.Sites[HostingEnvironment.ApplicationHost.GetSiteName()].Bindings)
{
// ...
}
}
Solution 2
Try this:
using (Microsoft.Web.Administration.ServerManager sm = Microsoft.Web.Administration.ServerManager.OpenRemote("localhost"))
{
int counter = 0;
string[] ipAddress = new string[10];
string[] sites = new string[10];
List<Tuple<string, string>> mylist = new List<Tuple<string, string>>();
foreach (var site in sm.Sites)
{
sites[counter] = site.Name;
foreach(var bnd in site.Bindings)
ipAddress[counter] = bnd.EndPoint != null ?
bnd.EndPoint.Address.ToString() : String.Empty;
mylist.Add(Tuple.Create(sites[counter], ipAddress[counter]));
counter++;
}
}
Comments
-
c00000fd over 1 year
Say, I have the "Site Name" web site in the IIS. I can access most of its functions via the ServerManager class from my C# code. What I can't seem to figure out is how to get the "Browse" URL for it, like I showed on the screenshot below?
If I go to Manage Website -> Browse in the IIS Manager, it will launch the IE with a URL as such:
http://localhost:8080/app1/Default.aspx
So I need to get a URL like that.
PS. Note that I don't need to launch the site itself.
-
SwissCoder almost 5 yearsBe aware that this "foreach(var bnd in site.Bindings)" will only add the last endpoint found.