类别和子类别的 mvc 路由示例

发布于 2024-10-22 06:18:37 字数 213 浏览 1 评论 0原文

我有一个静态网站(没有数据库),并且很难理解如何为子类别设置路线。例如,我可以执行以下操作,其中类别是控制器,品牌是操作:

  • 汽车/丰田
  • 汽车/bwm,

但是当我添加另一个级别时,我不知道如何设置路线

  • 汽车/丰田/凯美瑞
  • 汽车/丰田/卡罗拉
  • 汽车/丰田/塞利卡

i have a static website (no database) and am having difficulty understanding how to setup routes for sub-categories. for example, i can do the following where the category is the controller and the make is the action:

  • cars/toyota
  • cars/bwm

but when i add another level i don't know how to setup the route

  • cars/toyota/camry
  • cars/toyota/corolla
  • cars/toyota/celica

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

故人如初 2024-10-29 06:18:37

我可能会使用年份/品牌/型号

routes.MapRoute(
   "Default",
   "{controller}/{year}/{make}/{model}"
   new
   {
       controller = "car", 
       action = "search",
       year = DateTime.Today.Year,
       model = "all",
       make = Url.OptionalParameter
   }
);

(您可能想要对年份进行约束以强制其成为合理的值?),

使用像这样的控制器

public class CarController 
{

    public ActionResult Search( int year, string make, string model )
    {
         // handle model "all" and empty "make" specially
    }
}

I'd probably go with year/make/model

routes.MapRoute(
   "Default",
   "{controller}/{year}/{make}/{model}"
   new
   {
       controller = "car", 
       action = "search",
       year = DateTime.Today.Year,
       model = "all",
       make = Url.OptionalParameter
   }
);

(you might want a constraint on the year to force it to be a reasonable value?)

with a controller like

public class CarController 
{

    public ActionResult Search( int year, string make, string model )
    {
         // handle model "all" and empty "make" specially
    }
}
倒数 2024-10-29 06:18:37

您应该可以接受如下所示的路由:

routes.MapRoute(
   "CarsRoute",
   "cars/{make}/{model}",
   new { 
      controller = "Cars", 
      action = "Display", 
      make = UrlParameter.Optional, 
      model = UrlParameter.Optional 
   });

这将映射到具有签名的操作方法:

public ActionResult Display(string make, string model)

其中 makemodel 都可以为 null。然后您可以执行您的操作。

You should be ok with a route looking like this:

routes.MapRoute(
   "CarsRoute",
   "cars/{make}/{model}",
   new { 
      controller = "Cars", 
      action = "Display", 
      make = UrlParameter.Optional, 
      model = UrlParameter.Optional 
   });

This would map to an action method with the signature:

public ActionResult Display(string make, string model)

Where both make and model can be null. You can then perform your actions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文