Pages - Menu

nopCommerce - Register route for SEO Friendly Url Redirect

Scope

This document is scoped to nopCommerce 3.1. The latest version as of 2 months ago, now the latest stable nopCommerce is up to 3.2 as of today.

My goal today, is to add custom logics in the CatalogController, so that all SEO Friendly Url (generic path) will go to my custom controller not the default controller. Previously, I documented here on how I create custom controller. It is similar to what I have done before, but generic path are handled differently in nopCommerce.

Route

As documented in the official FAQ, we know how to register route for normal pages. For admin pages, I got that covered here. Today, we are going to look at how SEO Friendly pages work. 

SEO Friendly pages are the ones that does not follow the standard MVC url pattern.

In summary, there are 3 types of routes in nopCommerce.

http://mydomain/checkout/completed/
It is a normal page. It reference to the Checkout Controller and the Completed Action.

http://mydomain/Admin/setting/Catalog
It is an admin page. It is referencing to the Setting Controller and the Catalog Action from the admin area.

http://mydomain/women-s-logo-stretch-full-zip-hoodie
It is a SEO page. It is handled by a special route provider called GenericUrlRouteProvider. The string "women-s-logo-stretch-full-zip-hoodie" is called a SEO slug, usually referenced by the SeName property in some of the nopCommerce objects.

GenericUrlRouteProvider

GenericUrlRouteProvider is located in Nop.Web\Infrastructure.



By default, it has a priority set to -1000000, this is latest route by default, but of course you can override it. There are 2 type of methods are being called inside this file. MapGenericPathRoute and MapLocalizedRoute. In fact, they are not just different methods, but the methods are coming from different objects.

MapGenericPathRoute belongs to the object GenericPathRoute; and MapLocalizedRoute belongs to the object LocalizedRoute. And they have the following relationship.
public partial class GenericPathRoute : LocalizedRoute
public class LocalizedRoute : Route
There is also the following comment in this file by the core developer. This is a very important note and we will see in a minute.
//define this routes to use in UI views (in case if you want to customize some of them later)

GenericPathRoute 

As it is named, everything that are generic will get pass to this handler. All works are done inside this file. As in the source code, everything is hard coded.


This is a very important fact to know. The default behaviour of nopCommerce is not passing in any namespace, and the only way to change it is inside this piece of code.

This is located in the Nop.Web.Framework\Seo

In this example, I have added the following line, and now the .Net engine will look for the controller in the correct namespace.

data.DataTokens["Namespaces"] = new[] { "Nop.Web.Controllers.Custom" };

LocalizedRoute


LocalizedRoute handles the virtual path. It provides functionalities for the HTML helper to render any hyperlinks in the view (UI). Any changes here should be complimented by changes in GenericPathRoute.

This is located in the Nop.Web.Framework\Localization

Summary

MapGenericPathRoute would change the route provider behaviour such that when an seo-friendly url is requested, the right controller will be called in the system.

MapLocalizedRoute changes the way how the HTML helper would generate the url for links inside the view. Hence the comments from core team was saying "define this routes to use in UI views". 

6 comments:

  1. How will you achieve this through a plugin? It gets stucked somehow when I follow your logic through plugin. I copied GenericPathRoute and GenericPathRoute Extension classes and adjusted as your suggestion in the plugin. i cant distinguish between controllers whichones to be used from plugin which ones from nop.web.

    ReplyDelete
    Replies
    1. It can distinguish the difference between the 2 controllers because they have different namespace. You should have a read from this too. http://tech.sunnyw.net/2013/11/nopcommerce-customization-with.html

      Delete
  2. This comment has been removed by a blog administrator.

    ReplyDelete
  3. This was very very helpful. Thank you.
    Now my Topics can have products listed on them without affecting the Core code.

    ReplyDelete
  4. I will do niche blog comment Just in 5$ .All comment relevant with your niche and UNIQUE .This off-page seo will increase your traffic and promote your business.

    ReplyDelete