From the course: ASP.NET MVC: Building for Productivity and Maintainability

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Using extension methods to strongly type common actions

Using extension methods to strongly type common actions

From the course: ASP.NET MVC: Building for Productivity and Maintainability

Start my 1-month free trial

Using extension methods to strongly type common actions

- [Instructor] Now that I've already showed you how to use extension methods to replace magic strings in your application in this video I'm going to leverage that same exact approach of centralizing logic in extension methods, however, this time I'm going to create a strongly typed wrapper around the Url.action Url helper method to not only reduce the number of magic strings, but also the amount of duplicate codes scattered throughout my application. In order to demonstrate the problem and make it easier to fix I'll start by doing a global search for all the times the Url helper is used to generate links to a product page. As the search results show this same code is used in several places throughout the site complete with its magic strings. To fix this I'll start out like last time by creating an extension class. This time I'll name it UrlHelperExtensions. Then I'll create the static extension method named product which extends the system.web.MVC.Url helper class. This method is…

Contents