tags:

views:

37

answers:

1

I am very new to ASP.NET MVC and am working over an eCommerce application that uses ASP.NET MVC 2.

For all the scenario, the default route works fine. But I need to define my own rule for URL like below:

  1. http: //localhost/mvc2proj/products
  2. http: //localhost/mvc2proj/products/productname

For this, I defined a new route just above the default routing:

 routes.MapRoute(
        "Products", 
        "Products/{productName}",
        new { controller = "Products", action = "Index" }
 );

It works fine when home page gets displayed. That is when the URL is http: //localhost/mvc2proj, the anchor tag's href attribute for menu header "Products" is "href="mvc2proj/Products" Products".

But when the URL is http: //localhost/mvc2proj/products/productname, the anchor tag's href attribute for menu header "Products" also gets changed to href="mvc2proj/Products/productName" which is not obviously desired. The menu href should not be changed whenever Product's category is clicked individually. It should remain unchanged.

I defined menu as a User Control like this:

<li><%=Html.RouteLink("Home", new { Controller = "Main", Action = "Index" })%></li>
<li><%=Html.RouteLink("Products", new{ Controller = "Products" } ) %></li>

Product's subcategory is defined like this:

<%foreach(var product in Model.Products) {%>
    <li><%=Html.RouteLink(Html.Encode(product.ProductName), new { controller = "Products", productName = product.ProductName })%></li>
<%} %>

The HTML for subcategory is:

<li><a href="/mvc2proj/Products/Product1">Product1</a></li>
<li><a href="/mvc2proj/Products/product2">Product2</a></li>

....... and so on. Clicking on these links changes the menu URL.

Please help me.

A: 

This may not be the best solution, but I got around a similar issue by defining a different route for the Index page than for the individual product pages, i.e. something like:

routes.MapRoute(
"ProductIndex", 
"Products",
new { controller = "Products", action = "Index" }
);

This would be additional to your existing route. I think I actually used different Action methods for each, but there would be no need to if you supplied a default value for the parameter.

I am sure there is a better way and that this solution comes from a lack of understanding of what's really going on, but it may help you solve the immediate problem!

randomsequence
Thanx my friend. I have already implemented the solution similar to that you have proposed. But now the URL looks like http: //localhost/mvc2proj/products?productName=prod_name rather than http: //localhost/mvc2proj/products/prod_name. Is there any solution?Best Regards,Vikas Anand
That's because it's finding the "ProductsIndex" route before the one with the {productName} - try putting the {productName} one first, and not passing a product name when you go to the index route. Make sure your Html.RouteLinks are referring to the correct route too.Glad it helped a little, don't forget to accept the answer if it solved it ;) (shameless fishing there!)
randomsequence
Could you please explain it with an example? I am very new to ASP.NET MVC. :(
Put the route I gave you AFTER your route in the RegisterRoutes method.Then for the link to your Index page use Html.RouteLink("ProductIndex") [with any additional options you may need there, though note that you don't need to specify the Controller as you are in your question as it's already in the Route], and for the link to a specific product page use Html.RouteLink("Products", new { productName = "whatever" });.For the specific product link you may need to specify a couple of other parameters, I forget the exact overload you want. Hopefully that'll point you in the right direction!
randomsequence
Thanks a lot!!!!!! It worked. But may I know the reason why the navigation got changed when I click on product listing? Reason will be highly appreciated. Thanks once again.
MVC picks the *first* route in the routes table that matches, so when it found the "ProductIndex" route first (which doesn't contain the {productName} parameter) it added it to the URL via query string, so you get ?productName=xyz. There is a lot I don't understand about MVC routing too so I'm afraid I can't help you much more with a deeper understanding (yet!). Glad I was able to help though :)
randomsequence