Route Helpers Tutorial

This tutorial aims to illustrate the usage of the different utility methods provided in the RouteHelpers class in the DigitallyCreated.Utilities.Mvc assembly. It assumes knowledge of C# and ASP.NET MVC.


Sometimes it's useful to get a RouteValueDictionary containing the necessary route values to generate a URL back to the current page. This includes any URL query parameters (ie ?param=value). The RouteToCurrentPage extension method off of UrlHelper lets you do this. Here is an example of its use in a View:

<%= Html.RouteLink("Link to current page", Url.RouteToCurrentPage()) %>

Fluent Syntax Modification of RouteValueDictionary

Sometimes you need to modify a RouteValueDictionary inline in an expression, especially when working with HtmlHelpers or UrlHelpers. The Include (alias of Add), Exclude (alias of Remove) and Replace (alias for the indexer) let you do this, as they return the RouteValueDictionary, which allows you use chain up the method calls in the fluent syntax style.
Here's an example of their use:

<%= Html.RouteLink("Link to current page without sorting", Url.RouteToCurrentPage().Exclude("sort")) %>


The MapPhysicalToVirtualPath extension method off of HttpServerUtilityBase (or HttpServerUtility) lets you do the opposite of the HttpServerUtilityBase.MapPath method, namely get the virtual path for a file whose physical path you have.
Here's an example of its use:

<a href="<%= ResolveUrl(Context.Server.MapPhysicalToVirtualPath(Model.File.PhysicalPath)) %>">Download file</a>


WARNING: This method can be brittle as ASP.NET MVC doesn't really support the finding of an action method outside of an HTTP Request. This method mocks a fake request in order to determine authorization rights. If you get a NotImplementedException, you're probably doing something unexpected and unsupported with these mock request objects. In this case, you can either submit a patch to fix it, or stop using this method.
In addition, as the method effectively simulates a request in order to determine whether the user is authorized for that route, there may be a performance cost. This cost, whether it exists or not, has not been measured or tested and one should keep that in mind when choosing to invest in using this method. If in doubt, I personally suggest you don't use it.

The IsCurrentUserAuthorizedForRoute allows you to specify access rights to various features once and for all as Authorize attributes on your Controller and its action methods. In your views, you may be doing something similar to this:

<% if (User.IsInRole("Administrator")) { %>
    <%= Html.RouteLink("Admin Page", new { controller = "Admin", action = "Index" }) %>
<% } %>

You would also have the [Authorize(Roles="Administrator")] attribute on your action method/controller. However, if you want to change it so that the Developer role had access to the admin pages as well as the Administrator role, you'd need up update not only the attributes on your action method/controller, but also the code in your views. The IsCurrentUserAuthorizedForRoute method lets you just specify your access rights on the controller/action method using attributes, and allows you to use that data from within your view, removing the need for encoding those access rights into your view logic.
Here's how you would use the method:

<% if (Url.IsCurrentUserAuthorizedForRoute(new { controller = "Admin", action = "Index" }, HttpVerbs.Get)) { %>
    <%= Html.RouteLink("Admin Page", new { controller = "Admin", action = "Index" }) %>
<% } %>

Last edited Mar 23, 2010 at 3:49 PM by dchambers, version 1


No comments yet.