Search code examples
asp.netsearchpostgetquery-string

Bookmarking ASP.NET search results using POST or GET?


I need a little help understanding how HTML forms work. It is my understanding that forms that use GET as their method submit name/value pairs for all fields within the form tags of said submission. However, if you take a look at the follow example from Google (and I've seen this in many other places too) and only use one of the fields on the form:

http://books.google.co.uk/advanced_book_search

Rather than being sent to a page with a name/value pair for each field of the advanced search page you are taken to a much cleaner looking URL:

http://www.google.co.uk/search?tbo=p&tbm=bks&q=hitchiker&num=10

Despite all of the input fields on the advanced search page.


Onto my problem... My own advanced search page is quite large and at the moment is being POSTed to my search results page which is taking in the values and searching accordingly, no problems! However, I want my users to be able to bookmark/share their searches and in order to do this I need to have items being passed into the querystring but I don't want massive querystrings if I don't need them. If my user has only searched by a color for example then I want the URL to be something like search.aspx?color=red; If they're searching by color and size then search.aspx?color=red&size=large and so on. Is this possible?

To complicate things even further I'm using ASP.NET so it's not the easiest of things to create a form that uses GET though I do believe I have already found away around this.

If you can give any advice or a nudge in the right direction, then thank-you! :)


Solution

  • What you're suggesting should be easily possible if you conditionally check the querystring on the results page to ensure the key/value is there.

    if(Request.QueryString["color"] != "")
    {
        // Add color to the seach parameters
    }
    

    To create the GET request I would think you would need to POST back to your search form and redirect to the results form from there, dynamically adding key/values to the querystring as and when they are required. This Post/Redirect/Get design pattern is typically used with web forms to help with book marking.