In an MVC project, the URL for any given handler depends upon where the web application is deployed. While running out of Visual Studio, a handler like MyController.Index() will have a url like http://localhost:5555/MyController/Index. If the web application is deployed to its own IIS website the url will be http://hostname.com/MyController/Index, but if it is deployed as a virtual application in a shared IIS website the url would be http://hostname.com/virtappname/MyController/Index. Finally, UrlHelper methods like Url.Action("Index", "MyController") will return a url path that's relative to the deployment path: /MyController/Index.

To get the absolute URL for the root of the web application, use this:

var homeUrl = VirtualPathUtility.ToAbsolute(Url.Content("~/"));

Url.Content() will parse the ~/ at the start of a url string and expand it based on the application's deployment root; either / or /virtappname/ in the examples above. Url is the current UrlHelper object, which is available inside controllers and views. In other classes you can create a helper:

var urlHelper = new UrlHelper(HttpContext.Current.Request.RequestContext);
var homeUrl = VirtualPathUtility.ToAbsolute(urlHelper.Content("~/"));

You can use any of the UrlHelper methods here, such as Url.Action().

var homeUrl = VirtualPathUtility.ToAbsolute(Url.
Action("Index", "MyController"));

However, when using Action() there is a shortcut: the fourth parameter, which is optional, can be used to specify the HTTP scheme to use (http or https). When that is specifed, Action() will return an absolute url instead of a url path that's relative to the deployment root.

var homeUrl = Url.
Action("Index", "MyController", null, Request.Url.Scheme);

Request is also made available in controllers and views. In other classes use HttpContext.Current.Request.Url.Scheme.