skip to Main Content

I have a page created on VS13 MVC 5 based on ASP.NET, on my navigator I have my dropdown menu

enter image description here

While I click on Phones link I have URL as: www.sitename.com/phones and it will list all phones which I have on my data base, while I choose a brand (e.g: Samsung) I want my URL will be www.sitename.com/phones/samsung

Simple question I don’t want create a view for each brand or model of phone because there are a lot, I can just use SQL and list them on the same page but how to change my URL depending on my choice ? Is the only way is Attribute Routing?

Appreciate any suggestion.

2

Answers


  1. If you are using JS/jQuery you can use the History.js api. https://github.com/browserstate/history.js/

    You can then use History.pushState(insert parameters here);. This will not only change the url to your choice but also allow users to go back and forward like usual, it also keeps SEO with web crawlers I think.

    Login or Signup to reply.
  2. You can add a Route to your route config

    in RouteConfig.cs

     routes.MapRoute(
                name: "Product",
                url: "{controller}/{productCategory}/{productName}",
                defaults: new { controller = "Product", action = "Index", productCategory = UrlParameter.Optional, productName = UrlParameter.Optional }
            );
    

    in product controller

    public ActionResult index(string productCategory,string productName)
    {
         //return items by productCategory/ProductName
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search