Can anyone help me? I’m doing some tests in ASP.NET MVC.
I want to implement the nice URL as stackoverflow.com routing system.
For example:
stackoverflow.com/questions/1074/asp-mvc
domain.com/id/title
Here is my code:
- in the Global.asax
routes.MapRoute(null,
"posts/{PostId}/{Title}",
new { controller = "Posts", action = "Details", post = (string)null },
new { PostId = @"\d+", Title = @"([A-Za-z0-9-]+)" }
);
- in the View:
<%= Html.ActionLink(Model.Title, "Details", new { Model.PostID, Model.Title})%>
With those codes, I am geting the url : http://localhost:53171/posts/Details?PostID=5&Title=Test-title
Can one one advise me?
Thank you very much.
Not sure what all of the stackoverflow url means, but if you wanted a clean url like:
https://stackoverflow.com/Questions/132/thetitle
In your Global.asax:
In your controller retrieve the values:
To produce the correct url use Html.RouteLink: