Saturday, February 12, 2011

How do you use querystrings with ASP.NET routing?

The new ASP.NET routing is great for simple path style URL's but if you want to use a url such as:

http://example.com/items/search.xhtml?term=Text+to+find&page=2

Do you have to use a catch all parameter with a validation?

  • You can match querystring parameters with routes as well, if you want to just capture everything you need to add a parameter like so:

    {*contentUrl}

    Which will populate the rest of the url into that variable.

    From Duncan
  • Any view data items that are not listed in the route are automatically mapped to the querystring, so if you map "items/search.xhtml" to an action:

    Search(string term, int page)
    

    Then you should get the results you are looking for.

    JarrettV : There isn't any great documentation about how this works, thanks for the answer. I wonder why the MVC team thinks querystrings are so bad, i think they make sense in cases of searching and paging
    Atømix : In many cases, query strings aren't bad, it's just that people will be tempted to overuse them instead of using RESTful Urls. with Searching I can see them making sense, but paging? Why not do this: http://site.com/products/page/4

0 comments:

Post a Comment