MVC2 - Route mapped but QueryString rendered
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 开发者_JS百科SearchController correctly executes also. I really would rather have the cleaner URL rendered by my ActionLink. What have I missed?
Thanks in advance.
As I don´t have your model, I did remove the foreach loops and I replace all unknown values by strings. In my tests I found the opposite behavior: the first link was ok while the other was not cleaner. The fix for the second action link was to replace "product" by "productcode".
<ul>
<li><%= Html.ActionLink("Category", "Results", "Search", new { category= "Test Category A", query = "test product", page=2 }, null)%></li>
</ul>
<hr />
<table>
<tr>
<td>
<%= Html.ActionLink("Product", "Details", "Product", new { productcode = "1234ABC020848" }, new { })%><br />
</td>
</tr>
</table>
Both ways are suppose to work as the routing system is responsible for mapping the variables.
routes.MapRoute(
"SearchDefault",
"{controller}/{action}/{category}/{query}/{page}",
new { },
new { controller = "Search", action = "Results" }
);
This should get you url like this (some additional tweaks may be required)
http://localhost/Search/Results/Test%20Category%20A/test%20product/1
Ignore that... I didn't see the first route in the question.
I usually like to use query string for searches tho, because once you got two or more parameters that aren't required, routes for that are kinda pain to build and maintain.
精彩评论