Understanding New Routes Registration in nopCommerce ASP.NET (MVC)

Registering new routes (ASP.NET Routing)

With the help of ASP.NET routing, you can map URLs that do not have to map directly to web files in your website. Basically, the ASP.NET routing module is responsible for mapping all the incoming browser requests to particular MVC controller actions.

For more detailed information check the following:

The ASP.NET MVC framework and ASP.NET Dynamic Data extend routing to provide features that are used only in MVC applications and in Dynamic Data applications. For more information about MVC, see ASP.NET MVC 3. For more information about Dynamic Data, see ASP.NET Dynamic Data Content Map.

nopCommerce is based on ASP.NET (MVC) and it follows the IRouteProvider interface for registering the routes during the application start event. All the routes are registered in the "Route Provider" class in the Nop.Web project.

In order to view the "Route Provider" class, go to: Nop.Web/Infrastructure/RouteProvider.cs.

solution explorer

You will see the route registration of the homepage:

  1. using System.Web.Mvc;  
  2. using System.Web.Routing;  
  3. using Nop.Web.Framework.Localization;  
  4. using Nop.Web.Framework.Mvc.Routes;  
  5.   
  6. namespace Nop.Web.Infrastructure   
  7. {  
  8.    public partial class RouteProvider: IRouteProvider   
  9.    {  
  10.      public void RegisterRoutes(RouteCollection routes)   
  11.       {  
  12.        //We reordered our routes so the most used ones are on top. It can improve performance.   
  13.       //home page   
  14.       routes.MapLocalizedRoute("HomePage",  
  15.        "",  
  16.        new   
  17.          {  
  18.           controller = "Home", action = "Index"  
  19.          },  
  20.           new []   
  21.            {  
  22.             "Nop.Web.Controllers"  
  23.            });  
You can use this "Route Provider" class for the following:

  • Plugins custom routes
  • New custom pages

For example: In plugins, you need to create a new class while implementing the IRouteProvider interface. After that, simply register the routes hat is specific to your new plugin.

Some other examples of how route is registered in nopCommerce are as follows:

  1. //widgets   
  2. //we have this route for performance optimization because named routes are MUCH faster than usual Html.Action(...)   
  3. //and this route is highly used   
  4. routes.MapRoute("WidgetsByZone",   
  5. "widgetsbyzone/",   
  6. new  
  7.    {   
  8.      controller = "Widget", action = "WidgetsByZone"  
  9.    },   
  10. new[]   
  11.    {  
  12.      "Nop.Web.Controllers"   
  13.    });   
  14.   
  15. //login   
  16. routes.MapLocalizedRoute("Login","login/",   
  17. new   
  18.    {   
  19.      controller = "Customer", action = "Login"   
  20.    },   
  21. new[]   
  22.    {  
  23.      "Nop.Web.Controllers"   
  24.    });   
  25. //register   
  26. routes.MapLocalizedRoute("Register","register/",   
  27. new   
  28.    {  
  29.      controller = "Customer", action = "Register"   
  30.    },   
  31. new[]   
  32.    {   
  33.      "Nop.Web.Controllers"   
  34.    });   
  35. //logout   
  36. routes.MapLocalizedRoute("Logout","logout/",   
  37. new   
  38.    {   
  39.      controller = "Customer", action = "Logout"   
  40.    },   
  41. new[]   
  42.    {  
  43.      "Nop.Web.Controllers"   
  44.    });   
  45.   
  46. //shopping cart   
  47. routes.MapLocalizedRoute("ShoppingCart","cart/",   
  48. new   
  49.    {  
  50.      controller = "ShoppingCart", action = "Cart"   
  51.    },   
  52. new[]   
  53.    {  
  54.      "Nop.Web.Controllers"  
  55.    });   
  56. //wishlist   
  57. routes.MapLocalizedRoute("Wishlist","wishlist/{customerGuid}",   
  58. new   
  59.    {  
  60.      controller = "ShoppingCart", action = "Wishlist", customerGuid = UrlParameter.Optional   
  61.    },   
  62. new[]   
  63.    {  
  64.      "Nop.Web.Controllers"   
  65.    });   
Explanation

  1. //login   
  2. routes.MapLocalizedRoute("Login"// ROUTE NAME "login/", // URL   
  3.    new   
  4.     {  
  5.         controller = "Customer", action = "Login"  
  6.     }, // PARAMETERS DEFAULTS   
  7.     new []   
  8.      {  
  9.         "Nop.Web.Controllers"  
  10.     });  
Ebook Download
View all
Learn
View all