0

我将下一个 MapRoute 添加到我的 RouteConfig 中:

 routes.MapRoute(
            name: "Noticias",
            url: "{controller}/{action}/{id}/{urlAmiga}",
            defaults: new
                {
                    controller = "Noticias",
                    action = "Noticia",
                    id = "",
                    urlAmiga = ""
                }
            );

在视图中,我有下一个:

 <a href="<%= Url.Action("Noticia","Noticias", new {id = item.IdNoticia, urlAmiga="this-is-a-test" }) %>">
                                <h5>
                                    <%= item.Titulo %>
                                </h5>
                            </a>

控制器:

 public ActionResult Noticia(int id)
    {
        var noticia = new NoticiaRepository().RecuperarNoticiaPorId(id);
        ViewBag.IdNoticia = noticia.Id;
        return View("Noticia", noticia);
    }

我希望生成的链接看起来像:

http://www.domain/Noticia/​​1123/this-is-a-test

但是,结果是:

http://www.domain/Noticia?id=106532&urlAmiga=this-is-a-test

为什么?

4

1 回答 1

0

确保您已在默认路由之前声明了此路由:

routes.MapRoute(
    name: "Noticias",
    url: "{controller}/{action}/{id}/{urlAmiga}",
    defaults: new
    {
        controller = "Noticias",
        action = "Noticia",
        id = "",
        urlAmiga = ""
    }
);

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new
    {
        controller = "Home",
        action = "Index",
        id = UrlParameter.Optional,
    }
);
于 2013-08-01T07:22:39.123 回答