Notice: Basic working understanding of MVC routing is assumed for this post – this can be not a tutorial on what routing is or the way it works, but instead a lightweight analysis of how NopCommerce plugin routing is architected
Routing can at times be a tough or confusing topic in .NET development. There are a lot of parts that match collectively to produce things perform proper, specifically with MVC. When working having a massive system like NopCommerce, this really is especially accurate due to the need for extensibility. NopCommerce enables builders to write their particular plugins and integrate them to the NopCommerce routing method. Including routes into your Plugin can be complicated, so let’s have a look at how this all functions. Beneath I’ve pasted a sample route in the NopCommerce plugin tutorial.
routes.MapRoute("Plugin.Payments.PayPalStandard.PDTHandler", "Plugins/PaymentPayPalStandard/PDTHandler", new { controller = "PaymentPayPalStandard", action = "PDTHandler" }, new[] { "Nop.Plugin.Payments.PayPalStandard.Controllers" } );
This is a fairly normal MVC (and NopCommerce) route. MapRoute is a shorthand technique that returns a brand new Route item that implements the default MVC Route Handler. If we quickly examine the parameters to be able we will see there’s nothing special heading on here:
- The identify from the route, in this case described by the namespace and class.
- The url route the plugin maps to
- The default values for your controller and motion. In this scenario the controller and motion aren’t variable sections in the URL – they’ll always be PaymentPayPalStandard and PDTHandler for that URL.
- Finnaly we pass within an anonymous object that specifices a namespace. This can be essential in MVC projects and it is definitely essential in NopCommerce. This parameter instructs MVC to prioritize controllers discovered in the namespace we handed in for the provided route. For example, in case your MVC venture has two home controllers, you’ll be able to recognize which one a specific route should use by specifying a namespace. This turns into more and more important in extensible scenarious like NopCommerce, in which numerous plugins may need the same controller title this kind of as “AdminController” or some thing. Like a aspect note, in the event you pass in numerous Namespaces they may be handled with equivalent priority, meaning if you have two namespaces you want to pass in as very first and next priority you’ll need to outline two routes.
So basically, make sure to contain a namespace parameter in your routes for MVC plugins – this aids Nop determine that the controller will be the correct one to use to your route and avoids collisions with other plugins.
Knowing How Plugin Routes are identified and registered
Let’s now take a look at how NopCommerce actually discovers, registers and makes use of all of the various routes that plugins can define.
Inside most MVC assignments, routes are registered in both the App_Start/RouteConfig.cs file (in MVC 5 and 6) or simply in the Application_Start approach from the global.asax file. Application_Start is one of several different software existence cycle occasions that are fired every time a brand new application instance is started. This isn’t some thing specific to MVC – this really is just part of the ASP.NET system. Remember, routing in general is part of ASP.NET and isn’t MVC specific. NopCommerce is basically no different on this regard.
Nop.Web will be the venture that really operates while you’re using NopCommerce, therefore if we glance inside this project we should see a worldwide.asax file. Let us open it up and browser towards the Application_Start() approach. In the center of the method we must always see a line of code that registers our routes.
RegisterRoutes(RouteTable.Routes);
The RegisterRoutes system is where the NopCommerce magic starts to happen. Let’s think about the center two strains in particular beneath the remark “//register custom routes (plugins, etc)”. Very first NopCommerce works by using its dependency injection motor to request an occasion of an object that implements IRoutePublisher, which in this instance is RoutePublisher.cs. We then call the routePublisher’s possess RegisterRoutes technique . Bear in mind, it is a different RegisterRoutes strategy as opposed to just one we are contacting in worldwide.asax. We once more pass while in the RouteTable.Routes assortment we passed in to the very first RegisterRoutes approach to produce the RouteTable offered for introducing routes. Let us see what occurs within the RoutePublisher.cs RegisterRoutes system.
public virtual void RegisterRoutes(RouteCollection routes) { var routeProviderTypes = typeFinder.FindClassesOfType<IRouteProvider>(); var routeProviders = new List<IRouteProvider>(); foreach (var providerType in routeProviderTypes) { //Ignore not installed plugins var plugin = FindPlugin(providerType); if (plugin != null && !plugin.Installed) continue; var provider = Activator.CreateInstance(providerType) as IRouteProvider; routeProviders.Add(provider); } routeProviders = routeProviders.OrderByDescending(rp => rp.Priority).ToList(); routeProviders.ForEach(rp => rp.RegisterRoutes(routes)); }
This is really where all of the power of NopCommerce plugin routing resides. This method calls another very important method – the only other method defined in RoutePublisher.cs. We can see the FindPlugin method below:
protected virtual PluginDescriptor FindPlugin(Type providerType) { if (providerType == null) throw new ArgumentNullException("providerType"); foreach (var plugin in PluginManager.ReferencedPlugins) { if (plugin.ReferencedAssembly == null) continue; if (plugin.ReferencedAssembly.FullName == providerType.Assembly.FullName) return plugin; } return null; }
Let us review what’s likely on right here in these two classes. Within our RegisterRoutes technique, NopCommerce uses the typeFinder and C# reflection to discover all of the courses that put into action the interface IRouteProvider. All NopCommerce plugins should possess a RouteConfig.cs file that implements IRouteProvider, which defines a RegisterRoutes technique. Right after it’s gathered each of the kinds that apply this interface, it then utilizes the FindPlugin approach to examine if the corresponding plugin for every RouteProvider is installed. In the event the plugin really is installed, it proceeds to produce an instance of every course that implements IRouteProvider and calls the RegisterRoutes method of every. This effectively loops through each of the plugins within your NopCommerce set up and provides each of the described plugin routes towards the RouteTable.
All these routes are then registered with the software during the Application_Start lifecycle event and may be used by the ASP.NET routing module to match incoming requests for the routes you’ve described (the MVC Routing Module is over and above the scope of this post).
For example, the FacebookShop plugin defines a RouteProvider course that implements IRouteProvider and defines the following RegisterRoutes method :
public void RegisterRoutes(RouteCollection routes) { //home page routes.MapRoute("Plugin.Misc.FacebookShop.Index", "facebook/shop/", new { controller = "MiscFacebookShop", action = "Index" }, new[] { "Nop.Plugin.Misc.FacebookShop.Controllers" } ); //category page routes.MapRoute("Plugin.Misc.FacebookShop.Category", "facebook/shop/category/{categoryId}/", new { controller = "MiscFacebookShop", action = "Category" }, new { categoryId = @"\d+" }, new[] { "Nop.Plugin.Misc.FacebookShop.Controllers" } ); //search page routes.MapRoute("Plugin.Misc.FacebookShop.ProductSearch", "facebook/shop/search/", new { controller = "MiscFacebookShop", action = "Search" }, new[] { "Nop.Plugin.Misc.FacebookShop.Controllers" } ); }
This plugin adds three routes, all of which will be added to the RouteTable when an instance of this RouteProvider class is created and the RegisterRoutes method is called. The URLs defined in those routes will then be available for use in the application.
Best and Cheap NopCommerce Discount Windows Hosting
ASPHostPortal.com has great experience in providing the best E-commerce Hosting support for their many happy customers. Whatever your budget, E-commerce Hosting with ASPHostPortal.com means affordable and reliable Windows hosting. You will enjoy the full support of the experienced ASPHostPortal.com team, 24 hours a day, 7 days a week. Affordable Budget prices, full features, 99.9% Uptime Guarantee, No Risk with 30 day Money-Back Guarantee for the best NopCommerce Hosting services!
[stextbox id=”asphostportal”]ASPHostPortal.com is Microsoft No #1 Recommended Windows and ASP.NET Spotlight Hosting Partner in United States. Microsoft presents this award to ASPHostPortal.com for the ability to support the latest Microsoft and ASP.NET technology, such as: WebMatrix, WebDeploy, Visual Studio 2012, .NET 4.5.2/ASP.NET 4.5.1, ASP.NET MVC 6.0/5.2, Silverlight 5 and Visual Studio Lightswitch. Click here for more information[/stextbox]
ASPHostPortal.com revolutionized hosting with Plesk 12 panel, a Web-based interface that provides customers with 24×7 access to their server and site configuration tools. Some other hosting providers manually execute configuration requests, which can take days. Plesk 12 completes requests in seconds. It is included free with each hosting account. Renowned for its comprehensive functionality – beyond other hosting control panels – and ease of use, Plesk 12 Panel is available only to ASPHostPortal.com’s customers.