views:

476

answers:

2

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&amp;query=test%20product&amp;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.

A: 

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.

Caline
A: 
Necros