Skip to main content

Improve manipulation of MVC action URLs

Improve manipulation of MVC action URLs

This tip will prevent us from having to manipulate strings. Therefore :

  • It reduces mistakes in controller/action names and RouteValueDictionary keys
  • It allows to type strongly parameters (and convert if needed) that will be add to RouteValueDictionary values
  • It makes refactoring and renaming operations easier and safer.

Add MvcAction Class

We will implement a MvcAction class containing the information needed to point to an action.

Add extension methods for UrlHelper and HtmlHelper

We then add extension methods for UrlHelper and HtmlHelper to support MvcAction objects

Add ControllerUrlHelper

We create static class ControllerUrlHelper for each Controller to centralized MVC action :

(In this case for ContactController)

Example of use :

In layout main menu

In table