URL routing

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
10 years ago
Hi I wonder how the URL routing works in Nopcommerce

for example
http://localhost:15536/productNameA

routing code
routes.MapLocalizedRoute("Product",
                                     "{SeName}",
                                     new {controller = "Catalog", action = "Product"},
                                     new[] {"Nop.Web.Controllers"});

the controller code to display one product
[NopHttpsRequirement(SslRequirement.No)]
        public ActionResult Product(int productId)
        {
            var product = _productService.GetProductById(productId);
            //////////////////
            //////////////////
            //////////////////
            return View(model.ProductTemplateViewPath, model);
        }

how does it work that the product name gets converted into an id?? My friend sets some break points and told me there is some extension involved, please point me to the relevant classes in the project and give me the name of technik in use , so I can google it and learn??

Regards
wei
10 years ago
weihang wrote:
Hi I wonder how the URL routing works in Nopcommerce

for example
http://localhost:15536/productNameA

routing code
routes.MapLocalizedRoute("Product",
                                     "{SeName}",
                                     new {controller = "Catalog", action = "Product"},
                                     new[] {"Nop.Web.Controllers"});

the controller code to display one product
[NopHttpsRequirement(SslRequirement.No)]
        public ActionResult Product(int productId)
        {
            var product = _productService.GetProductById(productId);
            //////////////////
            //////////////////
            //////////////////
            return View(model.ProductTemplateViewPath, model);
        }

how does it work that the product name gets converted into an id?? My friend sets some break points and told me there is some extension involved, please point me to the relevant classes in the project and give me the name of technik in use , so I can google it and learn??

Regards
wei


Try this article below. I've written it to explain the ins and outs of URL Routing in nopCommerce. Enjoy!

http://www.pronopcommerce.com/nopcommerce-id-less-url-structure-demystified-how-does-nopcommerce-270-and-280-resolve-urls
10 years ago
Thanks, now I understand better
10 years ago
weihang wrote:
Thanks, now I understand better


No problem! :)
10 years ago
Hi, I try to debug a bit more and have one more question. (I am j2ee developer trying to learn .NET, so forgive me if I misunderstand how things work in .NET)

As I understand, all route providers extending IRouteProvider are registered only once at application start (Global.asax.cs.RegisterRoutes(RouteCollection routes)) and route objects get saved for application scope.

If I understand right from your blog post, the logic flow goes like this
1.A new request comes in, HTTP request data get processed by the function
public override RouteData GetRouteData(HttpContextBase httpContext)
{
}
In Nop.Web.Framework.Seo.GenericPathRoute
2.The manipulated request scope RouteData object containing the controller, action, id data is fed to the routing engine. So the cached route object will be used to invoke the corresponding controller methods.

The problem is that GetRouteData(HttpContextBase httpContext) does not get invoked all the time. It only gets invoked when the mapping is not found from the routing engine, for example
http://localhost:15536/search
http://localhost:15536/newproducts

So please spend bit more time explaining it behaves like that?
10 years ago
weihang wrote:
Hi, I try to debug a bit more and have one more question. (I am j2ee developer trying to learn .NET, so forgive me if I misunderstand how things work in .NET)

As I understand, all route providers extending IRouteProvider are registered only once at application start (Global.asax.cs.RegisterRoutes(RouteCollection routes)) and route objects get saved for application scope.

If I understand right from your blog post, the logic flow goes like this
1.A new request comes in, HTTP request data get processed by the function
public override RouteData GetRouteData(HttpContextBase httpContext)
{
}
In Nop.Web.Framework.Seo.GenericPathRoute
2.The manipulated request scope RouteData object containing the controller, action, id data is fed to the routing engine. So the cached route object will be used to invoke the corresponding controller methods.

The problem is that GetRouteData(HttpContextBase httpContext) does not get invoked all the time. It only gets invoked when the mapping is not found from the routing engine, for example
http://localhost:15536/search
http://localhost:15536/newproducts

So please spend bit more time explaining it behaves like that?


I think there's some misconception here. It is not true that "It only gets invoked when the mapping is not found from the routing engine". For a path to be taken by the route engine, it must have already been properly registered in the route table, only then is the route engine able to pick up the routes to do routing. GenericPathRoute is only part of the routing system, but there's another part - the normal Route. Look at Nop.Web.Infrastructure.RouteProvider. :)
10 years ago
Hi Lam,

Interesting post... clarifies a lot. Quick question... Previous to 3.0, I was using 1.9, and like many, I enhanced 1.9 a lot before making the jump to MVC (directly to 3.0 skipping all of 2.x).  In 1.9 I disabled URL Rewriting and used URL Routing instead as I wanted very clean urls with no IDs.  I had mappings like site/Product/ProductA and site/Category/CategoryA

From 1.9 Global file (custom code)
void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute(
             "category-browse",       // Route name
             "Category/{SEName}",         // URL  
             "~/Category.aspx" // Web Forms page  
        );
        
        routes.MapPageRoute(
         "product",       // Route name
         "product/{SEName}",         // URL  
         "~/Product.aspx" // Web Forms page  
        );
....


So after reading this, I wanted to do a bit of exploring... and got a bit stumped with this.  Can you elaborate a bit more?
In my situation, I want to either map with a route to the product / category routes, but I think I need to still touch the urlrecord/slug table...or would I be better off to do url rewriting for a legacy url in this situation?

Thanks
Chuck
10 years ago
ChuckR wrote:
Hi Lam,

Interesting post... clarifies a lot. Quick question... Previous to 3.0, I was using 1.9, and like many, I enhanced 1.9 a lot before making the jump to MVC (directly to 3.0 skipping all of 2.x).  In 1.9 I disabled URL Rewriting and used URL Routing instead as I wanted very clean urls with no IDs.  I had mappings like site/Product/ProductA and site/Category/CategoryA

From 1.9 Global file (custom code)
void RegisterRoutes(System.Web.Routing.RouteCollection routes)
    {
        routes.MapPageRoute(
             "category-browse",       // Route name
             "Category/{SEName}",         // URL  
             "~/Category.aspx" // Web Forms page  
        );
        
        routes.MapPageRoute(
         "product",       // Route name
         "product/{SEName}",         // URL  
         "~/Product.aspx" // Web Forms page  
        );
....


So after reading this, I wanted to do a bit of exploring... and got a bit stumped with this.  Can you elaborate a bit more?
In my situation, I want to either map with a route to the product / category routes, but I think I need to still touch the urlrecord/slug table...or would I be better off to do url rewriting for a legacy url in this situation?

Thanks
Chuck


Hi Chuck,

You mean you want to keep the URL, but implement it on top of 3.10 source code? :)
10 years ago
Hi Lam,

Yes exactly. Appreciate any input or pointers you could give me on how to port this to 3.1

Chuck
10 years ago
ChuckR wrote:
Hi Lam,

Yes exactly. Appreciate any input or pointers you could give me on how to port this to 3.1

Chuck


Have you read my article referenced in previous reply? Because if you understand that you'll be able to modify the route to do whatever you want. :D
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.