Create action links with different routes?
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
);
I expected to开发者_StackOverflow社区 see link like /Post/Test/asdf/3 but it is /Post/Test/3?title=3 Why ? How can i fix it?
I would suggest cleaning your code a bit, because there are many things done based on conventions. So keeping code consistent often helps.
public ActionResult Test(string title ,int id) // Order is switched and title changed to string
Edit: The problem is with wrong route path. You have to change it to "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 }
);
Btw: If you are going to play with routes a bit more, Phil Haack's blog will be a great resource.
精彩评论