我可以从站点地图生成 ASP.NET MVC 路由吗?

发布于 2024-07-04 06:42:50 字数 578 浏览 4 评论 0原文

我正在考虑为即将进行的项目学习 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空心空情空意 2024-07-11 06:42:50

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.

起风了 2024-07-11 06:42:50

扎克,如果我理解正确的话,你想要子类别的无限深度。 没什么大不了的,自从 MVC Preview 3(我认为是 3 或 4)以来,这个问题已经解决了。

的路由

只需为以下网址定义一条类似“{controller}/{action}/{*categoryPath}”

http://example.com/shop/products/household/kitchen/cookware/cooksets/nonstick

你应该有一个带有 Products 操作的 ShopController :

public class ShopController : Controller
{
...
    public ActionResult Products(string categoryPath)
    {
        // the categoryPath value would be
        // "household/kitchen/cookware/cooksets/nonstick". Process it (for ex. split it)
        // and then decide what you do..
        return View();
    }

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 :

public class ShopController : Controller
{
...
    public ActionResult Products(string categoryPath)
    {
        // the categoryPath value would be
        // "household/kitchen/cookware/cooksets/nonstick". Process it (for ex. split it)
        // and then decide what you do..
        return View();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文