开发者

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 { c开发者_StackOverflow社区ontroller = "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?


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"];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜