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?
You can determine your server's root URL by browsing to a page on your website and observing the address in the location/address entry field of the browser. The root URL is the section between the colon-slash-slash (://) and the next slash (/), omitting any port number (:portno).
A root URL is the start or index page of a domain on a web server. Colloquially, many users call the homepage the “root URL” as well.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With