Asynchronous JavaScript with async/await

In this course we will learn how to use the ES2017 async and await keywords to write asynchronous code that is more readable and easier to follow than equivalent code based on long promise chains or deeply nested callbacks.

Watch the Course

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!

Learn Node