.Net MVC 路由用于未确定数量的文件夹(嵌套类别结构)
我正在探索在我的下一个电子商务网站中使用 MVC 的可能性。我似乎无法弄清楚的一件事是我是否可以使用我通常使用的相同 URL 约定。目前,任何产品的 URL 都可以是以下之一:
- Category/SubCategory/Product1.html
- Category/SubCategory/SubSubCategory/Product2.html
- Category/SubCategory/SubSubCategory/Product3.html
- Category/SubCategory/SubSubCategory/SubSubSubCategory/Product4.html
等等。
我遇到的问题是嵌套类别结构。到目前为止,我想到的唯一一件事如下:
routes.MapRoute(
"Products",
"{categories}/{productname}",
new { controller = "Product", action = "Details", productname = UrlParameter.Optional },
new { categories = @"\w+/\w+" }
);
我希望 {categories} 可以与以下任何一项相匹配,我可以对其进行处理以识别产品所属的正确类别:
- Sport /Tennis/Rackets/ProductA
- Sport/Badminton/Rackets/ProductB
但上面显示的路线无法正常工作。
有谁知道如何实现这一目标,或者是否无法实现?
I'm exploring the possibility of using MVC for my next e-commerce site. One thing I can't seem to figure out is whether or not I can use the same URL convention I normally use. Currently, the URL for any product could be one of the following:
- Category/SubCategory/Product1.html
- Category/SubCategory/SubSubCategory/Product2.html
- Category/SubCategory/SubSubCategory/Product3.html
- Category/SubCategory/SubSubCategory/SubSubSubCategory/Product4.html
etc.
The issue I'm having is with the nested category structure. So far the only thing I've come up with is as follows:
routes.MapRoute(
"Products",
"{categories}/{productname}",
new { controller = "Product", action = "Details", productname = UrlParameter.Optional },
new { categories = @"\w+/\w+" }
);
I was hoping that {categories} could be matched with any one of the following which I could process to identify the right category that the product belongs to:
- Sport/Tennis/Rackets/ProductA
- Sport/Badminton/Rackets/ProductB
But the route shown above doesn't work correctly.
Does anyone know how this can be achieved, or if it can't be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以在 MVC2 中使用 Areas
所以它会这样:
区域/控制器/视图/id
所以在你的情况下它最终会是:
运动是区域,
网球控制器,
球拍视图,
ProductA 是 ID 或查询字符串,
http://www.asp.net/ mvc/videos/aspnet-mvc-2-areas
希望这是有道理的。
You could use Areas in MVC2
So it would read:
Area/Controller/View/id
So in your case it would end up being:
Sport being the area,
Tennis The controller,
Rackets the view,
ProductA being an ID or querystring,
http://www.asp.net/mvc/videos/aspnet-mvc-2-areas
Hope this makes sense.
路由系统允许您定义包罗万象的参数,忽略斜杠并捕获
直到 URL 末尾的所有内容。通过在参数前面加上前缀来将参数指定为包罗万象的参数
星号 (*)。
URL 模式中只能有一个包罗万象的参数,并且它必须是最后一个(即,
最右边)的东西,因为它捕获从该点开始的整个 URL 路径。
但需要注意的是,它不会从查询字符串中捕获任何内容,因为路由对象只查看
URL 的路径部分。
如果您让访问者浏览某种任意类型的内容,Catchall 参数会很有用。
深度层次结构,例如在内容管理系统 (CMS) 中。
您可以使用 RouteData 对象来提取有关路线的信息。根据您的需要,您可能会创建一个自定义路由处理程序来解析路由数据并调用正确的控制器方法。
The routing system allows you to define catchall parameters, which ignore slashes and capture
everything up to the end of a URL. Designate a parameter as being catchall by prefixing it with an
asterisk (*).
You can only have one catchall parameter in a URL pattern, and it must be the last (i.e.,
rightmost) thing in the URL, since it captures the entire URL path from that point onward.
One Caveat though, it doesn’t capture anything from the query string as route objects only look at the
path portion of a URL.
Catchall parameters are useful if you’re letting visitors navigate through some kind of arbitrary
depth hierarchy, such as in a content management system (CMS).
You can use the RouteData object to extract information about the route. For your needs, you would probably create a custom route handler that parses the route data and calls the correct controller methods.
您需要访问 URL 的各个部分,因此需要将类别部分分为两个部分。那会让事情变得容易得多。
假设我们将网球和羽毛球类别以及这些类别中的球拍称为产品类,
您需要一种方法来访问类别、productClass 和 ProductName 参数。假设这种情况下“Sport”是固定的,我会这样做
:);
你的操作方法将是这样的
}
You need access to the individual segments of the URL so you need to divide the category segment into two segments. That would make it much easier.
Let's say we call Tennis and Badminton categories and Rackets within those categories as a product class
You need a way to access the category, productClass and productName parameters. Supposing that "Sport" is fixed in this case, I will do it like this:
);
Your action method will be something like this
}