:::: MENU ::::

Base URL in ASP.Net MVC3 / Razor

If you are doing AJAX calls inside a MVC3 applications (with Razor), you will frequently need to know the base URL of the site.

Especially if you are using non-standard port numbers, using Request.Url.Authority appears like a good lead at first.

However, it will fail in a load balanced environment (it will give you the actual web server, not the load balancer).

Instead, use this:

var urlHelper = new UrlHelper(Html.ViewContext.RequestContext);
var baseurl = urlHelper.Content(“~”);

So, what do you think ?