Nilzor
Nilzor

Reputation: 18593

How do I pass query parameters on in the MVC default route?

I have an MVC2 project where I want the default route to pass on query parameteres to the default action.

Right now I have a route like this

routes.MapRoute(
     "Default", // Route name
     "", // URL pattern
     new { controller = "Search", action = "Index" }
);

If I now go to http://mysite, that is correctly routed to the Index action on the Search controller, but if enter http://mysite?theme=yellow, then the theme parameter is not passed to the same action.

How can I make a default route that passes on any given query parameter?

Upvotes: 0

Views: 1824

Answers (1)

Rickard
Rickard

Reputation: 2335

routing has nothing to do with query parameters. They will be accessible from the controller that matches the url pattern.

so, "{controller}/{action}/{id}", // URL pattern should suffice

and in the controller you can get to query parameters through the Request property:

var theme = Request["theme"];

or you can be explicit if you want to:

var theme = Request.QueryString["theme"];

Upvotes: 1

Related Questions