public class PostController : YourDefinitionController
{
public ActionResult Test(int id ,int title)
{
return View();
}
}
@Html.ActionLink("Postss", "Test","Post" ,new { title = "asdf",id=3 },null)//in Razor view
// here is route registration
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Post",
"{Post}/{Test}/{title}/{id}",
new { controller = "Post", action = "Test",id=UrlParameter.Optional, title=UrlParameter.Optional }
);
routes.MapRoute(
"Defaultx", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
我希望看到像/ Post / Test / asdf / 3这样的链接,但它是/ Post / Test / 3?title = 3 为什么?我该如何解决?
答案 0 :(得分:1)
我建议稍微清理一下代码,因为根据约定做了很多事情。因此,保持代码一致通常会有所帮助。
public ActionResult Test(string title ,int id) // Order is switched and title changed to string
编辑:问题在于错误的路径路径。您必须将其更改为"Post/Test/{title}/{id}"
// changed route path expression
routes.MapRoute(
"Post",
"Post/Test/{title}/{id}",
new { controller = "Post", action = "Test", title = UrlParameter.Optional, id = UrlParameter.Optional }
);
顺便说一句:如果您打算多玩一些路线,Phil Haack's blog将是一个很好的资源。