ASP.NET MVC Extension Methods of UrlHelper

I think this is kind of obvious, but I guess it might not be for everyone because I’ve seen some code where people do not use this! URL Helpers are really easy to setup, it takes only a few minutes and will probably save you a lot of massive Search & Replace in the future!

public static string Image(this UrlHelper helper, string fileName)
    return helper.Content("~/Content/Images/" + fileName));

public static string Stylesheet(this UrlHelper helper, string fileName)
    return helper.Content("~/Content/Stylesheets/" + fileName);

public static string Script(this UrlHelper helper, string fileName)
    return helper.Content("~/Content/Scripts/" + fileName);

So instead of doing this:

<link href="../../../Content/StyleSheets/Main.css" rel="stylesheet" type="text/css" />

You can do this:

<link href="<%= Url.Stylesheet("Main.css")%>" rel="stylesheet" type="text/css" />

These are just 3 common examples, you should do the same thing for things you use a lot.

This entry was posted on Monday, September 21st, 2009 at 9:31 pm and is filed under ASP.NET, MVC. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

13 Responses to “ASP.NET MVC Extension Methods of UrlHelper”

kvr September 22nd, 2009 at 9:09 am

I like the Image extension method, but if you use the StyleSheet or Script method you loose intellisense in Visual Studio.

Jeff Fritz September 22nd, 2009 at 10:28 am

Personally, I like to add UrlHelper extensions for some of my more commonly used routes.. it helps to clarify my ASPX code

Chris December 8th, 2009 at 12:37 pm

Seems a tad daft using code for a very static element…

Nice example of how to make other helpers though

Seth Petry-Johnson January 25th, 2010 at 11:12 am

I do this all the time. In fact, if I have commonly used images (like status icons) I’ll often create an Enum for them and then create a helper that knows how to convert that enum to its correct image path.

@Chris, the point is that he’s decoupling the name of a resource from the specific place it’s stored on disk. This has a few benefits:

1) Easier to read the markup, especially if the resource paths are very long.

2) Easier to change resource locations over time. I’d rather change one helper than do a text search/replace over my site.

3) Easier to add “skinning” ability to the site. To implement user-specific skins, you’d need only update the helper methods to read in the current user profile and use the appropriate skin directory. No changes needed to the markup code.

Mike January 25th, 2010 at 11:52 am

@Chris, Like Seth said, using Helpers make a much nicer markup and it will help you make changes quickly in the future.

Marlon Maulsby April 22nd, 2010 at 6:27 pm

what namespaces do I need to include in order for this to function as is implied here?

Mike April 23rd, 2010 at 10:07 am

@Marlon You should only need System.Web.Mvc

Marlon Maulsby April 29th, 2010 at 11:14 am

@Mike, Thank you for the Response Mike. However I had already included that namespace. The error I received, is saying that the ‘System.Web.Mvc.UrlHelper’ does not contain a definition for ‘Routename’. My first assumption is that the URLhelper is not properly extended.

Handy Extension Methods for ASP.NET MVC’s UrlHelper | OOP - Object Oriented Programing May 4th, 2010 at 5:19 pm

[...] Chambaud posted three extension methods he created for UrlHelper: Image(), Stylesheet() and Script().  They make it pretty easy to [...]

dervlap June 1st, 2010 at 1:54 pm

Just for info,

You can call it with Url and not UrlHelper and do not forget to remove any namespace where the extensions are.

Kamil December 10th, 2010 at 4:43 am

It is really helpful, thanks for your post

c# mvc urlhelper image example | my Album April 15th, 2011 at 9:39 pm

[...] ASP.NET MVC Extension Methods of UrlHelper | MIKE Sep 21, 2009 … NET MVC Extension Methods of UrlHelper”. kvr September 22nd, 2009 at 9:09 am. I like the Image … [...]

download script January 4th, 2012 at 8:14 pm

download script…

[...]ASP.NET MVC Extension Methods of UrlHelper | MIKE[...]…

Leave a Reply