MiBol
MiBol

Reputation: 2125

How get root url path

I'm searching a way to get the root URL of my Web project; as example:

Local:

http://localhost:52390/pages/user.aspx

Expected result: http://localhost:52390

IIS:

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

Answers (3)

Elim Garak
Elim Garak

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

MiBol
MiBol

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

Troy Harris
Troy Harris

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

Related Questions