我将下一个MapRoute添加到我的RouteConfig:
routes.MapRoute(
name: "Noticias",
url: "{controller}/{action}/{id}/{urlAmiga}",
defaults: new
{
controller = "Noticias",
action = "Noticia",
id = "",
urlAmiga = ""
}
);
在View中,我有下一个:
<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
为什么?
答案 0 :(得分: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,
}
);