Hi this my ActionLink
@foreach (var item in Model)
{
<div>
<h3>
@Html.ActionLink(item.Title, "Post", new { postId = item.Id, postSlug = item.UrlSlug })
</h3>
</div>
}
and also this is Post action result
public ActionResult Post(Guid postId, string postSlug)
{
var post = _blogRepository.GetPostById(postId);
return View("Post", post);
}
and finally I have defined this route in global.asax to support the above action
routes.MapRoute("PostSlugRoute", "Blog/{Post}/{postId}/{postSlug}",
new
{
controller = "Blog",
action = "Post",
postId = "",
postSlug = ""
});
what I get in Url is this
http://localhost:1245/Blog/Post?postId=554c78f1-c712-4613-9971-2b5d7ca3e017&postSlug=another-goos-post
but I don't like this ! I expect something like this
http://localhost:1245/Blog/Post/554c78f1-c712-4613-9971-2b5d7ca3e017/another-goos-post
what should I do to achieve this ??
Change your Route definition to not have Post be a parameter.
routes.MapRoute("PostSlugRoute",
"Blog/Post/{postId}/{postSlug}", // Removed the {} around Post
new { controller = "Blog", action = "Post", postId = "", postSlug = "" }
);
And ensure your route is above MVC's Default route.
UPDATE: Updating with exact examples I used
global.asax
routes.MapRoute("PostSlugRoute",
"Blog/Post/{postId}/{postSlug}", // Removed the {} around Post
new { controller = "Blog", action = "Post", postId = "", postSlug = "" }
);
~/Views/Blog/Post.cshtml
@{
Guid id = Guid.Parse("554c78f1-c712-4613-9971-2b5d7ca3e017");
string slug = "another-goos-post";
string title = "Another Goos Post";
}
@Html.ActionLink(title, "Post", new { postId = id, postSlug = slug })