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.
You will see the route registration of the homepage:
- using System.Web.Mvc;
- using System.Web.Routing;
- using Nop.Web.Framework.Localization;
- using Nop.Web.Framework.Mvc.Routes;
-
- namespace Nop.Web.Infrastructure
- {
- public partial class RouteProvider: IRouteProvider
- {
- public void RegisterRoutes(RouteCollection routes)
- {
-
-
- routes.MapLocalizedRoute("HomePage",
- "",
- new
- {
- controller = "Home", action = "Index"
- },
- new []
- {
- "Nop.Web.Controllers"
- });
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:
-
-
-
- routes.MapRoute("WidgetsByZone",
- "widgetsbyzone/",
- new
- {
- controller = "Widget", action = "WidgetsByZone"
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
-
-
- routes.MapLocalizedRoute("Login","login/",
- new
- {
- controller = "Customer", action = "Login"
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
-
- routes.MapLocalizedRoute("Register","register/",
- new
- {
- controller = "Customer", action = "Register"
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
-
- routes.MapLocalizedRoute("Logout","logout/",
- new
- {
- controller = "Customer", action = "Logout"
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
-
-
- routes.MapLocalizedRoute("ShoppingCart","cart/",
- new
- {
- controller = "ShoppingCart", action = "Cart"
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
-
- routes.MapLocalizedRoute("Wishlist","wishlist/{customerGuid}",
- new
- {
- controller = "ShoppingCart", action = "Wishlist", customerGuid = UrlParameter.Optional
- },
- new[]
- {
- "Nop.Web.Controllers"
- });
Explanation
-
- routes.MapLocalizedRoute("Login",
- new
- {
- controller = "Customer", action = "Login"
- },
- new []
- {
- "Nop.Web.Controllers"
- });
- Version used for this article: nopCommerce 3.60.
- You can download nopCommerce here.