6 ASP.NET MVC UrlHelper Quick Tips to Maximize Your Link Management
October 17th, 2014
Use these 6 tips to make your URL management easier in ASP.NET MVC 4.
When you first got into web development and developed your first web site, it was probably a simple 2 or 3 page site, probably static, and a LOT of links.
If a page's URL changes, you would have to change every link on every page that referenced that URL. Not cool!
So over the years, I've found a number of ways to lower your stress when managing your links across your 1000-page website (What? You don't have one?) ;-)
Here are my top 6 ways to manage your links across your own ASP.NET MVC website.
- Place your links in one place.
When I mean place your links in one location, I don't mean place them into one HTML View. I mean logically partition your links in a Helpers\Url folder by class or, if you have a small web site, place them into one class for easy reference.
When you're asked to change a pages' location and that means touching hundreds of pages of links to change, you learn this tip...REAL QUICK! :-) - Remove your Url.ActionLinks from your Views.
I would say that 60% of every ASP.NET MVC application I've seen, a majority of people use the good old Url.ActionLink syntax when building their links on a View.
Instead of using Url.ActionLinks, replace them with<a href="@Url.RootUrl()">
Why? 5 reasons:
- It makes your HTML code more granular
- It makes your code more readable
- It makes your code compile-able with no "magic strings"
- It removes the human error factor from fat-fingering a link
- It refers back to tip 1 which implies that all links are in one centralized location.
- Use #regions
I can just hear all of the developers groaning and moaning about regions, but I find them helpful when I have a large list of URLs that I can easily see at-a-glance where everything is located in the class. As I've said before, it's like a table-of-contents for link management. - Use Url.RouteUrl whenever possible
The Url.RouteUrl is the fastest generation of a Url in ASP.NET MVC. Period. There were tests done comparing the different types of link generation and Url.RouteUrl was the fastest out of all of them. I have seen it's speed on a link directory web site I wrote back in 2012. After replacing the Url.ActionLink with Url.RouteUrl, there was a significant speed boost. - Overload your UrlHelpers
There are times when you need to pass in an object as opposed to a string or integer, so why not use both. It doesn't hurt to have both. So if you have a FAQ link like below:
public static string FaqPageUrl(this UrlHelper helper) { return helper.RouteUrl("Default", new { @controller = "About", @action = "FAQ" }); }
There's no harm in adding this one as well (so long as you need it).
public static string FaqPageUrl(this UrlHelper helper, int number) { return helper.RouteUrl("Default", new { @controller = "About", @action = "FAQ", @id=number }); }
So not only can you have a link that goes to your FAQ page, you can also have a link that goes to a FAQ number loaded from the id as well. Here's how you'd use it in the example below:<a href="@Url.FaqPageUrl(2)" title="Going To FAQ number 2 page">
- Use common UrlHelpers in every project.
If there are common links you use, place these UrlHelper routines in your scaffolding to speed up your development. For example, here's one for a Root Url that will take you back to a main page.public static string RootUrl(this UrlHelper helper) { return helper.Content("/"); }
I hope these UrlHelper tips make your link management a little easier on you when developing your ASP.NET MVC web site.
Happy Coding!