Reputation: 6428
I want to write a little helper method which returns the base URL of the site. This is what I came up with:
public static string GetSiteUrl()
{
string url = string.Empty;
HttpRequest request = HttpContext.Current.Request;
if (request.IsSecureConnection)
url = "https://";
else
url = "http://";
url += request["HTTP_HOST"] + "/";
return url;
}
Is there any mistake in this, that you can think of? Can anyone improve upon this?
Upvotes: 202
Views: 317012
Reputation: 327
You can use UriHelper.BuildAbsolute
UriHelper.BuildAbsolute(HttpContext.Request.Scheme, HttpContext.Request.Host, HttpContext.Request.PathBase);
Upvotes: 0
Reputation: 3385
This works for me.
Request.Url.OriginalString.Replace(Request.Url.PathAndQuery, "") + Request.ApplicationPath;
So if you want to access your domain name do consider to include the application name in case of:
====================================
For the dev.x.us/web
it return this strong text
Upvotes: 1
Reputation: 11
Please use the below code
string.Format("{0}://{1}", Request.url.Scheme, Request.url.Host);
Upvotes: 1
Reputation: 19
I'm using following code from Application_Start
String baseUrl = Path.GetDirectoryName(HttpContext.Current.Request.Url.OriginalString);
Upvotes: 1
Reputation: 924
Based on what Warlock wrote, I found that the virtual path root is needed if you aren't hosted at the root of your web. (This works for MVC Web API controllers)
String baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority)
+ Configuration.VirtualPathRoot;
Upvotes: 4
Reputation: 39289
The popular GetLeftPart
solution is not supported in the PCL version of Uri
, unfortunately. GetComponents
is, however, so if you need portability, this should do the trick:
uri.GetComponents(
UriComponents.SchemeAndServer | UriComponents.UserInfo, UriFormat.Unescaped);
Upvotes: 11
Reputation: 190907
This is a much more fool proof method.
VirtualPathUtility.ToAbsolute("~/");
Upvotes: 7
Reputation: 5537
I believe that the answers above doesn't consider when the site is not in the root of the website.
This is a for WebApi controller:
string baseUrl = (Url.Request.RequestUri.GetComponents(
UriComponents.SchemeAndServer, UriFormat.Unescaped).TrimEnd('/')
+ HttpContext.Current.Request.ApplicationPath).TrimEnd('/') ;
Upvotes: 7
Reputation: 5662
To me, @warlock's looks like the best answer here so far, but I've always used this in the past;
string baseUrl = Request.Url.GetComponents(
UriComponents.SchemeAndServer, UriFormat.UriEscaped)
Or in a WebAPI controller;
string baseUrl = Url.Request.RequestUri.GetComponents(
UriComponents.SchemeAndServer, UriFormat.Unescaped)
which is handy so you can choose what escaping format you want. I'm not clear why there are two such different implementations, and as far as I can tell, this method and @warlock's return the exact same result in this case, but it looks like GetLeftPart()
would also work for non server Uri's like mailto
tags for instance.
Upvotes: 6
Reputation: 7471
string baseUrl = Request.Url.GetLeftPart(UriPartial.Authority)
That's it ;)
Upvotes: 177
Reputation: 4392
Try this:
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority +
Request.ApplicationPath.TrimEnd('/') + "/";
Upvotes: 370
Reputation: 24515
you could possibly add in the port for non port 80/SSL?
something like:
if (HttpContext.Current.Request.ServerVariables["SERVER_PORT"] != null && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "80" && HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString() != "443")
{
port = String.Concat(":", HttpContext.Current.Request.ServerVariables["SERVER_PORT"].ToString());
}
and use that in the final result?
Upvotes: -2