Is it possible to get the base URL of a web site hosted in IIS 7 by using Microsoft.Web.Administration.ServerManager
?
In the simplest scenario, this would be:
http://localhost
but I need to get it programmatically.
If I can't use ServerManager
what is the best alternative?
To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.
API Host and Base URL. REST APIs have a base URL to which the endpoint paths are appended. The base URL is defined by schemes , host and basePath on the root level of the API specification. All API paths are relative to this base URL, for example, /users actually means <scheme>://<host>/<basePath>/users .
It is straightforward to get the package name of an Android app: Go to the Play store, find the app and view its page. Copy the URL from the browser address bar. Make a note of this as the "Android Fallback URL."
The base path is the initial URL segment of the API, and does not include the host name or any additional segments for paths or operations. It is shared by all operations in the API.
For those interested in the usage of Microsoft.Web.Administration.ServerManager, here's some code. Consider that an IIS application my have more than one binding, resulting in more than one URL per web application.
var siteName = "Default Web Site";
var appPath = "MyWebApplication";
var serverManager = new ServerManager();
var site = serverManager.Sites[siteName];
appPath = appPath.StartsWith("/") ? appPath : "/" + appPath;
var app = site.Applications[appPath];
var urls = new List<string>();
foreach (var binding in site.Bindings)
{
var sb = new StringBuilder();
sb.Append(binding.Protocol);
sb.Append("://");
if (!string.IsNullOrWhiteSpace(binding.Host))
{
sb.Append(binding.Host);
}
else
{
if (Equals(binding.EndPoint.Address, IPAddress.Any))
{
sb.Append("localhost");
}
else
{
sb.Append(binding.EndPoint.Address);
}
}
if (binding.EndPoint.Port != 80)
{
sb.Append(":");
sb.Append(binding.EndPoint.Port);
}
sb.Append(app.Path);
urls.Add(sb.ToString());
}
You can use string baseURL = HttpContext.Current.Request.Url.Host
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With