How to get the root Url

get root url javascript
php get base url
php get base url from string
root url vs base url
php get root directory of website
root url sharepoint
php get current url
php get current directory url

The code below is a dreadful hack.

Uri linkUri = HttpContext.Current.Request.Url;
string link = linkUri.ToString().Substring(0, linkUri.ToString().IndexOf("Users/Create"));

Instead of editing the string, how do I get the correct route Url in the first place?

For example I want to get http://localhost:9999/ instead of http://localhost:9999/Users/Create

It's pretty ugly, but how about:

Uri uri = new Uri("http://localhost:9999/Users/Create");
string link = string.Format("{0}://{1}:{2}", uri.Scheme, uri.Host, uri.Port);

Edit: or even better:


Get root url of the site in PHP, is the section between the colon-slash-slash (://) and the next slash (/), omitting any port number (:portno). SwiftView Inc. WAN and web licenses are locked to the website’s root URL, and to purchase one of these products you must send us your website’s root URL. 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.

You could use the Content method of UrlHelper:

string root = urlHelper.Content("~/");

What is my website's root URL?, gives you the domain name through which the current request is being fulfilled and is more directly related to the request. Say you have an host defined in Apache with ServerName of and an IP address of 1.2. 3.4 . 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:

Making Sense of ASP.NET Paths - Rick Strahl's Web Log

How about this? Request.ApplicationPath

How do I get the base URL with PHP?, A root URL is the start or index page of a domain on a web server. It must get information from the web server about which directory has been defined as root  Step 1: go to your default internet browser and login to your bitbucket account. Step 2: launch sourcetree and click on bitbucket cloud, automatically launch your default browser and as you are already connected it authenticates you directly. Here is the solution. Work for me.

var rootUrl = System.Web.HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority);

PHP: $_SERVER variables: $_SERVER['HTTP_HOST'] vs $_ , to get a specific part of URL. Think of a URL, http://localhost/testjs/index.html. I want to extract both the base URL and the root URL which are, The root site URL for SharePoint Online is provisioned based off your input when setting up your Office 365 subscription. For example, if your organization name is Contoso, the root site for SharePoint Online will be The remainder of this document will utilize Contoso as the organization name for all examples.

How to Get Domain Name from URL in PHP, Depending on your server configuration, getting correct path information can be challenging. For example, PHP does not provide a variable  How to Get the Base URL in a Class Outside the Controller Let’s say that you have a business layer that sends emails in your application. You want to include links in your emails to the application, and you want to grab the root URL to make those.

Root URL, 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​  A root-relative URL starts with a / character, to look something like <a href="/directoryInRoot/fileName.html">link text</a>. The link you posted: <a href="fruits/index.html">Back to Fruits List</a> is linking to an html file located in a directory named fruits, the directory being in the same directory as the html page in which this link appears.

How to get Base URL or Root URL using Javascript, I'm able to get my pages to load. I'm really at a wits end here, and wondering how I would define a base URL and Root URL on a live server  If links are set referring to the homepage, they should either directly refer to the domain followed by a slash or in a different root directory, the corresponding root URL. This also applies to entries in search engines or directories.

  • Yes this is certainly an improvement. I will wait and see what other answers I get, but this is the best so far.
  • I get an error message; "An object reference is required for the non-static field method, or property 'System.Web.Mvc.UrlHelper.Content(string)'"
  • Yes, you need a reference to an UrlHelper. If you are writing this code in a controller you already have the Url property on which you could invoke the Content method. If it is in a view: <%= Url.Content("~/") %>.
  • I am not using this code in a View. I am coding within a class, where UrlHelper does not have a Content method.
  • you can use inside controller (new UrlHelper(this.ControllerContext.RequestContext)).Content("~/")
  • I only seem to be getting the "/"
  • I can only access the Request object as follows; HttpContext.Current.Request.ApplicationPath. All this returns is a "/".