Question

My old asp.net page had this kind of URL

localhost/Product/This-Is-The-Name-Of-The-Product/Item123.aspx

where the product ID would be 123, which I would get and then pass it along to the routing engine to grab the right data from the database and then display the page.

localhost/Product/This-Is-The-Name-Of-The-Product/Item456.aspx

would get product id of 456.

I'm rewriting the website in MVC and I was wondering if someone could tell me how I would do a route so that it would be backwards compatible and localhost/Product/This-Is-The-Name-Of-The-Product/Item123.aspx would go to localhost/Product/123

I'm using MVC 5 BTW.

Was it helpful?

Solution

U can use 2 routes together:

routes.MapRoute( //for localhost/Product/This-Is-The-Name-Of-The-Product/Item456.aspx
            name: "",
            url: "Product/{ProductName}/item{id}.aspx",
            defaults: new { controller = "Product", action = "Index", id= UrlParameter.Optional }
        );

        routes.MapRoute( //this url for localhost/Product/123
            name: "",
            url: "Product/{id}",
            defaults: new { controller = "Product", action = "Index", id = UrlParameter.Optional }
        );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top