0

我只是在学习 MVC 并且正在尝试创建一个 SEO 页面标题,因此我正在发送控制器的 ID 以及 SEO 的页面 slug。

我想看Recipe/Details/18/foobar

但下面的代码返回

结果/名称/18?name=foobar

我认为这是我的自定义路线的问题,但从所有 mt Research 来看,我认为我做得正确。任何帮助表示赞赏。

_部分视图

<a href="@Url.Action("Details", "Recipe", new {id = Recipe.ID, name = Recipe.Slug}, null)">

路由配置

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        routes.MapRoute(
            name: "Recipe",
            url: "{controller}/{action}/{id}/{name}",
            defaults: new {
                            controller = "Recipe",
                            action = "Details",
                            id = UrlParameter.Optional,
                            name = UrlParameter.Optional
                            }
        );

控制器

public ActionResult Details(int id)
4

1 回答 1

1

您应该在默认路由之前添加自定义路由。它们按顺序处理,因此在您的情况下,mvc 使用默认路由。

问候,米哈伊尔

于 2015-12-07T14:35:40.687 回答