I am again here with my foolish MVC routing questions.
I am creating an E commerce project and stuck at one point in routing. So far attribute routing working fine with following URL.
abc.com/Electronics/Audio/Portable Audio/iPods
in form of
Category/Subcategory/Subsubcategory/Types/productname{stuck here}
But now I need to show the product name and here I am stuck. Product can be at any level. Like
abc.com/Electronics/Apple iPod Nano
abc.com/Electronics/Audio/Apple iPod Nano
abc.com/Electronics/Audio/Portable Audio/Apple iPod Nano
abc.com/Electronics/Audio/Portable Audio/iPods/Apple iPod Nano
The problem is if I define category controller like following:
[Route("{category}/{productname?}")]
public ActionResult Category(string category,string productname)
{
//code
}
and subcategory controller like following
[Route("{category}/{subcategory}/{productname?}")]
public ActionResult Subcategory(string category,string subcategory,string productname)
{
//code
}
And so on...
I get an error of ambiguous routes because after clicking the link for subcategory(If there is no product name),the attribute routing routes to the category controller again as criteria of two parameters is fulfilled.
So how can I differentiate between these two controllers. Or any better idea is always welcome as I am nearly a newbie in routing and MVC.
Also please tell if there is any idea through which we can strictly define controllers to be mapped regardless of parameters matched or not,but keeping the URL form as described above.
Thank You.