LA.NET [EN]

Sep 23

The MVC platform – the UrlHelper class

Posted in ASP.NET MVC      Comments Off on The MVC platform – the UrlHelper class

Today we’re going to start looking at the “famous” (well, most of the time I call then infamous, but the one we’re looking today is cool) helper classes introduced by the MVC platform. We’re going to start with one of the most important ones: the UrlHelper class. As you’ve seen, you’re be able to access an instance of this class through the Url properties exposed by the ViewPage and ViewUserControl classes. In theses cases, you don’t need to worry about initializing the new class instance because those classes alredy have the code to do that.

You’ll use this class whenever you need to get the url for a specific controller’s action method. The class ends up using its RouteCollection property to give you that information. As you might expect, if you don’t initialize that property (which you really won’t, unless you have specific needs or are writing tests), it will end up using the RouteTable that gets built during the application start event.

You’ll find several interesting methods:

  • Action:  several overloads of this method which let you generate an url based on an action;
  • RouteUrl: several overloads that will return an url based on a specific route info. For instance, if you’ve named your routes (like I generally do), you can pick a route by name and get its url;
  • Content: used for getting the path to a specific resource. You can use this method and pass one of those “relative” urls (“~/something”) and you’ll get the correct url for that resource;
  • Encode: used for encoding an url;

Most of the time, you’ll end up using the Action or RouteUrl methods. Lets start with the Action method.

The simplest thing you can do is call that method and pass the name of an action method. Here’s the signature of that method:

public string Action(string actionName)

If you do that, you’ll end up getting an url based on a method found on the current controller (the current controller is found by calling the GetRequiredString over the RouteData object that is avaiable throught the ViewContext that was passed to the view). If the route associated with the method requires parameters,then you can specify those values by using one of the following overloads:

public string Action(string actionName,object values)
public string Action(string actionName, RouteValueDictionary valuesDictionary)

Do notice that with the 1st version you can pass ana nonymous object with the values you want to pass to the parameters used by your route.

You’re also able to use a different controller by using one of the following overloads:

public string Action(string actionName, string controllerName, object values)
public string Action(string actionName, string controllerName, RouteValueDictionary valuesDictionary)

And  if you’re really picky, you can still end up using another couple of overloads which let you specify the protocol (for instance, you might want to go into https!) and the hostname. In these cases, you’ll be wanting to use one of these:

public string Action(string actionName, string controllerName, object values, string protocol)
public string Action(string actionName, string controllerName, RouteValueDictionary valuesDictionary, string protocol, string hostName)

The RouteUrl method is really similar to this, but as I’ve said, with the available overloads you’re able to select a route by name. These method are really cool and you can use them whenever you need an url and you know the name of a route. This is probably the method I’ve used most times until now.

Before ending, one final note: don’t forget that the strings returned aren’t encoded. So, if you need to encode them pass them through the Encode method.

This is really a short post and I’m not putting any examples of using these methods here. If you think examples are a must, please let me know and I’ll put some here.

More to come on future posts. Keep tuned!