Question

We are building a products site and are using Routes in ASP.NET WebForms C#. We currently have

routes.MapPageRoute("", "{categoryName}", "~/Category.aspx");
routes.MapPageRoute("", "{categoryName}/{subCategoryname}", "~/SubCategory.aspx"); 

It works great ..

Shoes/Womens , Shoes/Kids , Boots/Mens , Boots/Womens.

We added a new Route in hopes to get the following resutls

Product/Mountain-Light-Mens-Hiking-Boots

So we now our Router has

routes.MapPageRoute("", "{categoryName}", "~/Category.aspx");
routes.MapPageRoute("", "{categoryName}/{subCategoryname}", "~/SubCategory.aspx"); 
routes.MapPageRoute("", "Product/{productName}", "~/Product.aspx");

However, when we go to /Product/Mountain-Light-Mens-Hiking-Boots the Router fires SubCategory.aspx, which is not what we want to happen. We would like the Product.aspx to fire up.

I understand that the Router does not know if 'Prodcut' is a categoryName or not.

How do I add another root level Routing for /Products ?

Was it helpful?

Solution

change your code order like this

routes.MapPageRoute("", "Product/{productName}", "~/Product.aspx");
routes.MapPageRoute("", "{categoryName}/{subCategoryname}", "~/SubCategory.aspx"); 
routes.MapPageRoute("", "{categoryName}", "~/Category.aspx");

Order is very important. Because you enter url like "www.domain.com/abc/abc" and if

routes.MapPageRoute("", "{categoryName}/{subCategoryname}", "~/SubCategory.aspx"); 

at third no. then this route never called. it call following route.

routes.MapPageRoute("", "{categoryName}", "~/Category.aspx");

And value will be.

string Value= Page.RouteData.Values["categoryName"].ToString();
Value= "abc/abc";

So, Url Pattern that Contain one Parameter should be at the and Route Collection.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top