How to Build Absolute Action URLs Using the UrlHelper Class

Building action URLs using the UrlHelper class shipping with ASP.NET MVC is very comfortable. Razor views can call the UrlHelper.Action() helper method as follows:

@Url.Action("About", "Home") @* Generates the URL /Home/About *@

However, UrlHelper.Action() only generates relative URLs when it is called the way shown above. You might ask why you would want to choose absolute URLs over relative ones. Relative URLs are perfectly fine — as long as they are called directly on your website. Problems arise if you want to provide, for example, an RSS feed because the items' <link> tags have to contain absolute URLs.

So how can absolute URLs be generated? ASP.NET MVC includes built-in functionality for the generation of absolute URLs, though not in a very intuitive way.

There are several overloads of UrlHelper.Action() method that enable you to pass additional parameters like route values, the protocol to use and the host name for the URL. If you are using any overload that allows you to specify the protocol, the generated URL will be absolute. Thus, the following code can be used to generate an absolute URL for the About action method of the HomeController:

@Url.Action("About", "Home", null, "http")

Since there are no route values to pass in this case, the third parameter is null. Given that your application is hosted on http://www.example.com, the method call above will return http://www.example.com/Home/About.

Although you can use the overload of UrlHelper.Action() as described above, I recommend to write a custom UrlHelper extension method for the sake of readability:

/// <summary>
/// Generates a fully qualified URL to an action method by using
/// the specified action name, controller name and route values.
/// </summary>
/// <param name="url">The URL helper.</param>
/// <param name="actionName">The name of the action method.</param>
/// <param name="controllerName">The name of the controller.</param>
/// <param name="routeValues">The route values.</param>
/// <returns>The absolute URL.</returns>
public static string AbsoluteAction(this UrlHelper url,
    string actionName, string controllerName, object routeValues = null)
{
    string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
 
    return url.Action(actionName, controllerName, routeValues, scheme);
}

Building an absolute URL using the UrlHelper.AbsoluteAction() extension method can be done with a clean and straightforward method call that does not contain redundancies (like the protocol) or null parameter values (like the route values):

@Url.AbsoluteAction("About", "Home")

Note that the readability and the understandability of the code increased because there are no more obscure null parameter values being passed.

If you are using a different protocol such as https, you can change the protocol constant within UrlHelper.AbsoluteAction() or, in case you are using multiple protocols within the same application, introduce either a new method overload or an optional parameter.

I have modified my code sample above according to Andrew's suggestion below. Instead of hard-coding "http" as the scheme to use, the sample now shows how to read and use the value of the current request's scheme. Therefore, the method does not have to be modified when a different scheme than "http" is used. Thanks, Andrew!

Use the coupon code LAUNCHDAY for $10 off!

Learn ES6

8 Comments

Andrew

Nice :)

Might I suggest using the 'Request.Url.Scheme' instead of hard-coding "http" - so this helper will work on "https" sites without needing to be modified?

It's not perfect because it relies on the requested URL (rather than the actual one the server would return - it is unlikely they'll be different, but they could..)

The return line would look like this:

return url.Action(actionName, controllerName, routeValues, url.RequestContext.HttpContext.Request.Url.Scheme);
Marius Schulz

Andrew, thank you for your suggestion! I have updated my code sample accordingly.

Dharmendra

@Url.AbsoluteAction("About", "Home") this method is calling on page load but I want to call this method when click on <a> tag. Please help me.

maddy

check list of Urlhelper overlaoded methods @ http://mvc4all.com/mvc/urlhelper-in-mvc/

rosdi

Thanks for this, it helps me built my own extension.. you saved a few hours day of my life.

Dinesh Kumar V

Thanks for the article. Though your article was not entirely related to what I was looking for, I was able to build upon from what you had provided. Thanks a lot. You have saved a lot of trouble for me.

Mayank Raval

How to Impalement Redirect URL in C# mvc5 ?

Freek Giele

Just a small remark. I'm currently working on a solution that uses loadbalancers. This causes the HTTPS protocol to be stripped when the request arrives at the webserver. (The header will contain a x-forwarded-for field) Using the Scheme of the request will cause your links to improperly show HTTP instead of HTTPS in that scenario.