Given the full URL, for example http://mydomain.com/MyApplication/Controller/Action , you can get some of this information from the System.Web.Hosting.HostingEnvironment.ApplicationHost object. You can get the following:
- ApplicationVirtualPath β "/ MyApplication"
- SiteName => "Default Website"
However, you will not be able to get the domain name before the real request appears. This is because the IIS website can accept requests for different domains, some of which are known only through DNS and are never configured anywhere.
For example, your site may respond to both mydomain.com and www.mydomain.com . Which one is the correct one that you want to add to your link?
You can configure your IIS website only to accept connections that request a specific host, but that cannot be received from an ASP.NET application.
source share