Hi there, in my Asp.net MVC2 app I have registered the following routes in the global.asax.cs:
routes.MapRoute(
"Search",
"Search/{action}/{category}/{query}/{page}",
new { controller = "Search", action = "Results", category = "All", page = 1 },
new { page = @"\d{1,8}" }
);
// URL: /Search
routes.MapRoute(
"SearchDefault",
"Search",
new { controller = "Search", action="Index" }
);
routes.MapRoute(
"Product",
"Product/{action}/{productcode}",
new { controller = "Product", action = "Details" }
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Search", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
I have my SearchController:
public ActionResult Results(string category, string query, int page)
{
}
I have my ProductController:
public ActionResult Details(string productcode)
{
return View();
}
In my Results.aspx View, the following ActionLinks exist:
<% foreach (var cat in Model.Categories) { %>
<li><%= Html.ActionLink(cat.Name, "Results", "Search", new { category= cat.Name, query = Model.SearchText, page=1 }, null)%></li>
<% } %>
</ul>
<hr />
<table>
<% foreach (var p in Model.Products) { %>
<tr>
<td>
<%= Html.ActionLink(p.ProductName, "Details", "Product", new { product = p.ProductCode }, new { })%><br />
</td>
</tr>
<% } %>
The first actionlink is rendering as:
"http://localhost/Search/Results?category=Test%20Category%20A&query=test%20product&page=1"
whereas the second ActionLink is correctly rendering:
"http://localhost/Product/Details/1234ABC020848"
The strange thing is that both work correctly and even if I manually type in:
"http://localhost/Search/Results/Test%20Category%20A/test%20product/1"
then my SearchController correctly executes also. I really would rather have the cleaner URL rendered by my ActionLink. What have I missed?
Thanks in advance.