两种不同的通用路由类型

发布于 2024-08-19 03:53:08 字数 1064 浏览 11 评论 0原文

我的 ASP.NET MVC 应用程序中有两种(到目前为止)不同类型的路由,一种是:{controller}/{action}/{id},另一种是 {controller}/{action}/{title}

目前我需要像这样定义路由:

        routes.MapRoute (
            "Default_Title_Slug",                                       // Route name
            "product/details/{title}",                          // URL with parameters
            new { controller = "product", action = "details", title = "" }      // Parameter defaults
        );

        routes.MapRoute (
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "site", action = "index", id = "" }  // Parameter defaults
        );

请注意,我必须将第一个路由绑定到产品控制器,这似乎是我可以让它工作的唯一方法...否则其他路由最终看起来像这样:

/controller/action?id=number

现在我需要添加另一个针对具有 {title} 段的另一个控制器的 MapRoute 调用...我不想为将来提出的每个特定条目创建新路线...是否有通用路线我可以创建映射 /controller/action/title ,以便与 /controller/action/id 路由很好地配合?

谢谢,
基隆

I've got two (so far) different types of routes in my ASP.NET MVC app, one is: {controller}/{action}/{id} and the other {controller}/{action}/{title}

Currently I need to define the routes like this:

        routes.MapRoute (
            "Default_Title_Slug",                                       // Route name
            "product/details/{title}",                          // URL with parameters
            new { controller = "product", action = "details", title = "" }      // Parameter defaults
        );

        routes.MapRoute (
            "Default",                                              // Route name
            "{controller}/{action}/{id}",                           // URL with parameters
            new { controller = "site", action = "index", id = "" }  // Parameter defaults
        );

Notice that the first one I've had to tie down to the product controller, this seems to be the only way I can get it work...otherwise the other routes end up looking like this:

/controller/action?id=number

Now I need to add another MapRoute call targeting another controller with the {title} segment...I don't want to create a new route for each specific entry I come up with in the future...is there a generic route I can create to map the /controller/action/title that'll play nicely with the /controller/action/id route?

Thanks,
Kieron

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

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

发布评论

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

评论(1

往事风中埋 2024-08-26 03:53:08

您可以使用路由约束(例如正则表达式)来做到这一点 - 一个非常类似的示例是 此处。像这样的东西:

routes.MapRoute (
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "site", action = "index", id = "" },
    new { id = @"\d+" }
);
routes.MapRoute (
    "Default_Title_Slug",
    "{controller}/{action}/{title}",
    new { controller = "product", action = "details", title = "" }
);

You can do that with a route-constraint, such as regex - a very similar example is here. Something like:

routes.MapRoute (
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "site", action = "index", id = "" },
    new { id = @"\d+" }
);
routes.MapRoute (
    "Default_Title_Slug",
    "{controller}/{action}/{title}",
    new { controller = "product", action = "details", title = "" }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文