Posted by: Cirilo Meggiolaro | 04/27/2009

Tip of the day #195 – ASP.NET MVC – UrlHelper class

The System.Web.Mvc.UrlHelper class contains a set of methods and properties to help you work with the ASP.NET MVC routes.

Constructor overloads

  • public UrlHelper(RequestContext requestContext);
  • public UrlHelper(RequestContext requestContext, RouteCollection routeCollection);

Properties

  • RequestContext RequestContext: Read-only property that encapsulates information about an HTTP request that matches a defined route;
  • RouteCollection RouteCollection: Read-only property that retrieves a collection containing the routes registered for the application;

Methods

  • string Action(string actionName): Returns a virtual path for the specified route values;
  • string Action(string actionName, object routeValues): Returns a virtual path URL for the specified route values;
  • string Action(string actionName, RouteValueDictionary routeValues): Returns a virtual path URL for the specified route values;
  • string Action(string actionName, string controllerName): Returns a virtual path URL for the specified route values;
  • string Action(string actionName, string controllerName, object routeValues): Returns a virtual path URL for the specified route values;
  • string Action(string actionName, string controllerName, RouteValueDictionary routeValues): Returns a virtual path URL for the specified route values;
  • string Action(string actionName, string controllerName, object routeValues, string protocol): Returns a fully qualified URL for the specified route values;
  • string Action(string actionName, string controllerName, RouteValueDictionary routeValues, string protocol, string hostName): Returns a fully qualified URL for the specified route values;
  • string Content(string contentPath): Converts a virtual path to an application absolute path;
  • string Encode(string url): Encodes a URL string;
  • string RouteUrl(object routeValues): Returns a virtual path for the specified route values;
  • string RouteUrl(RouteValueDictionary routeValues): Returns a virtual path for the specified route values;
  • string RouteUrl(string routeName): Returns a virtual path for the specified route values;
  • string RouteUrl(string routeName, object routeValues): Returns a virtual path for the specified route values;
  • string RouteUrl(string routeName, RouteValueDictionary routeValues): Returns a virtual path for the specified route values;
  • string RouteUrl(string routeName, object routeValues, string protocol): Returns a fully qualified URL for the specified route values;
  • string RouteUrl(string routeName, RouteValueDictionary routeValues, string protocol, string hostName): Returns a fully qualified URL for the specified route values.

Examples

You have at least two options to use the UrlHelper class:

  • You may create an instance by passing a RequestContext object to its constructor. Assuming we are in a controller class called SupplierController:

UrlHelper url = new UrlHelper(this.ControllerContext.RequestContext);
string s = url.Action(“Details”); // Returns /Supplier/Details
string s = url.Action(“Index”, “HomeController”); // Returns /HomeController

  • All Controller classes expose a property called Url that is an instance of UrlHelper that you may use it as you need.

string s = this.Url.Action(“Details”); // Returns /Supplier/Details
string s = this.Url.Action(“Index”, “HomeController”); // Returns /HomeController


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: