Reputation: 2125
I'm searching a way to get the root URL of my Web project; as example:
http://localhost:52390/pages/user.aspx
Expected result: http://localhost:52390
http://lst.pg.com/iLearn/pages/user.aspx
Expected result: http://lst.pg.com/iLearn
Exists a way to achieve this in ASPX? Or in Javascript/jQuery?
Upvotes: 9
Views: 29019
Reputation: 1817
There are times I need to get the rootpath from the code behind. Here is what I use.
public static string GetRootPath()
{
HttpContext CTX;
CTX = HttpContext.Current;
return CTX.Request.ApplicationPath.ToString();
}
Upvotes: 0
Reputation: 2125
ASPX: Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath
Implemented with Javascript:
<script type="text/javascript">
var rootUrl = '<% =(Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath) %>';
</script>
Upvotes: 2
Reputation: 475
in javascript you can get url information from the location object.
var href = location.href; //returns the entire url
var host = location.hostname; //returns just the hostname of the url.
Upvotes: 3