.Net MVC for an indefinite number of folders (nested category structure)

I am exploring the possibility of using MVC for my next e-commerce site. One thing that I seem to be unable to figure out is if I can use the same URL that I usually use. Currently, the URL for any product can be one of the following:

  • Category / SubCategory / Product 1.html
  • Category / SubCategory / SubSubCategory / Product 2.html
  • Category / SubCategory / SubSubCategory / Product 3.html
  • Category / SubCategory / SubSubCategory / SubSubSubCategory / Product 4.html

and etc.

The problem I am facing is related to the structure of the nested category. So far, the only thing I've come up with is the following:

routes.MapRoute(
    "Products",
    "{categories}/{productname}",
    new { controller = "Product", action = "Details", productname = UrlParameter.Optional },
    new { categories = @"\w+/\w+" }
);

, {categories} , , , :

  • // /ProductA
  • // /ProductB

, , .

- , , ?

+3
3

URL-, . .

,

category, productClass productName. , "", :

routes.MapRoute(
"Products",
"sport/{category}/{productClass}/{productName}",
new { controller = "Product", action = "Details", productClass = UrlParameter.Optional, productName = UrlParameter.Optional }

);

public ActionResult Details(string category, string productClass, string productName){
//Do whatever you need to do in order to get the specified product

}

0

, URL-. , (*).

routes.MapRoute(null, "Articles/{*articlePath}",
    new { controller = "Articles", action = "Show" }
);

catchall URL, ( , rightmost) URL-, URL- .

, , - , URL-.

Catchall , , , (CMS).

RouteData . , , , .

+5

You can use areas in MVC2

Therefore it will read: Area / Controller / View / identifier

So in your case it will be:

Sport is an area, Tennis Controller, Rackets view, ProductA - identifier or request,

http://www.asp.net/mvc/videos/aspnet-mvc-2-areas

Hope this makes sense.

0
source

Source: https://habr.com/ru/post/1784088/


All Articles