I want to get the absolute root Url of an ASP.NET application dynamically. This needs to be the full root url to the application in the form: http(s)://hostname(:port)/
I have been using this static method:
public static string GetSiteRootUrl()
{
string protocol;
if (HttpContext.Current.Request.IsSecureConnection)
protocol = "https";
else
protocol = "http";
StringBuilder uri = new StringBuilder(protocol + "://");
string hostname = HttpContext.Current.Request.Url.Host;
uri.Append(hostname);
int port = HttpContext.Current.Request.Url.Port;
if (port != 80 && port != 443)
{
uri.Append(":");
uri.Append(port.ToString());
}
return uri.ToString();
}
BUT, what if I don’t have HttpContext.Current in scope?
I have encountered this situation in a CacheItemRemovedCallback.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For WebForms, this code will return the absolute path of the application root, regardless of how nested the application may be:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + ResolveUrl("~/")
The first part of the above returns the scheme and domain name of the application (http://localhost) without a trailing slash. The ResolveUrl code returns a relative path to the application root (/MyApplicationRoot/). By combining them together, you get the absolute path of the web forms application.
Using MVC:
HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~/")
or, if you are trying to use it directly in a Razor view:
@HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority)@Url.Content("~/")
Method 2
You might try getting the raw URL and trimming off everything after the path forward slash. You could also incorporate ResolveUrl("~/").
Method 3
public static string GetAppUrl()
{
// This code is tested to work on all environments
var oRequest = System.Web.HttpContext.Current.Request;
return oRequest.Url.GetLeftPart(System.UriPartial.Authority)
+ System.Web.VirtualPathUtility.ToAbsolute("~/");
}
Method 4
public static string GetFullRootUrl()
{
HttpRequest request = HttpContext.Current.Request;
return request.Url.AbsoluteUri.Replace(request.Url.AbsolutePath, String.Empty);
}
Method 5
I’ve solved this by adding a web.config setting in AppSettings (“SiteRootUrl”). Simple and effective, but yet another config setting to maintain.
Method 6
UrlHelper url = new UrlHelper(filterContext.RequestContext);
string helpurl = url.Action("LogOn", "Account", new { area = "" },
url.RequestContext.HttpContext.Request.Url.Scheme);
Can get you the absolute url
Method 7
@saluce had an excellent idea, but his code still requires an object reference and therefore can’t run in some blocks of code. With the following, as long as you have a Current.Request the following will work:
With HttpContext.Current.Request
Return .Url.GetLeftPart(UriPartial.Authority) + .ApplicationPath + If(.ApplicationPath = "/", Nothing, "/")
End With
This will work no matter the protocol, port, or root folder.
Method 8
This has always worked for me
string root = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, "");
Method 9
Based off Uri’s but stripping query strings and handling when it is a virtual directory off IIS:
private static string GetSiteRoot()
{
string siteRoot = null;
if (HttpContext.Current != null)
{
var request = HttpContext.Current.Request;
siteRoot = request.Url.AbsoluteUri
.Replace(request.Url.AbsolutePath, String.Empty) // trim the current page off
.Replace(request.Url.Query, string.Empty); // trim the query string off
if (request.Url.Segments.Length == 4)
{
// If hosted in a virtual directory, restore that segment
siteRoot += "/" + request.Url.Segments[1];
}
if (!siteRoot.EndsWith("/"))
{
siteRoot += "/";
}
}
return siteRoot;
}
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0