Harshal Kor
Harshal Kor

Reputation: 51

How to rewriteurl in mvc 4

My project on mvc 4. there is search bar in my website. but when i searc in search bar the url is so long for eg. www.test.com/test/test?testname=abc

i want to show shot Url like example = wwww.test.com/search?q=abc how to change this Url

routes.MapRoute(
            name: "Product",
            url: "search?q=/{productName}",
            defaults: new { controller = "Products", action = "SearchResult" }
        );

this is not work for me. please help me

Upvotes: 1

Views: 33

Answers (2)

SNEHA chavan
SNEHA chavan

Reputation: 26

Try following code :

routes.MapRoute( name: "Product", url: "search/{action}", defaults: new { controller = "Products", action = "SearchResult" } );

Upvotes: 1

Do not include query string in the route. Asp.net MVC automatically maps the query string parameters to the parameters in action method in your controller.

Upvotes: 0

Related Questions