我可以从站点地图生成 ASP.NET MVC 路由吗?
我正在考虑为即将进行的项目学习 ASP.NET MVC 框架。 我可以使用高级路由基于站点地图层次结构创建长 URL 吗?
导航路径示例:
首页> 商店> 产品展示> 家庭> 厨房> 炊具> 炊具> 不粘
典型(我认为)MVC URL:
http://example.com/products/category/NonstickCooksets
所需网址:
http://example.com/shop/products/household/kitchen/炊具/炊具/不粘锅
我可以这样做吗?
I'm thinking of learning the ASP.NET MVC
framework for an upcoming project. Can I use the advanced routing to create long URLs based on the sitemap hierarchy?
Example navigation path:
Home > Shop > Products > Household > Kitchen > Cookware > Cooksets > Nonstick
Typical (I think) MVC URL:
http://example.com/products/category/NonstickCooksets
Desired URL:
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick
Can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MVC 路由允许您定义几乎任何您想要的结构,您只需要定义每个部分的语义含义即可。 您可以拥有“硬编码”的位,例如“商店/产品”,然后将其余部分定义为变量,“{category}/{subcategory}/{speciality}”等。
您还可以定义多个路由如果您愿意,所有都映射到相同的端点。 基本上,当 URL 进入您的 MVC 应用程序时,它会遍历路由表,直到找到匹配的模式,填充变量并将请求传递到适当的控制器进行处理。
虽然默认路由是简单的控制器、操作、Id 类型的设置,但这肯定不是您可以做的范围。
The MVC routing lets you define pretty much any structure you want, you just need to define what each of the pieces mean semantically. You can have bits that are "hard-coded", like "shop/products", and then define the rest as variable, "{category}/{subcategory}/{speciality}", etc.
You can also define several routes that all map to the same end point if you like. Basically, when a URL comes into your MVC app, it goes through the routing table until it finds a pattern that matches, fills in the variables and passes the request off to the appropriate controller for processing.
While the default route is a simple Controller, Action, Id kind of setup, that's certainly not the extent of what you can do.
扎克,如果我理解正确的话,你想要子类别的无限深度。 没什么大不了的,自从 MVC Preview 3(我认为是 3 或 4)以来,这个问题已经解决了。
的路由
只需为以下网址定义一条类似“{controller}/{action}/{*categoryPath}”
:
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick
你应该有一个带有 Products 操作的 ShopController :
Zack, if I understand right you want unlimited depth of the subcategories. No biggie, since MVC Preview 3 (I think 3 or 4) this has been solved.
Just define a route like
"{controller}/{action}/{*categoryPath}"
for an url such as :
http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick
you should have a ShopController with a Products action :