Tuesday, August 18, 2009

Get Querystring parameters in Page Webmethods ASP.NET

Today, at my work I have to find a way on how to get the query string parameters and use them in logic. I mean, I can get them in javascript side and send them as parameters, but I don’t want to implement it that way as I have everything in query string, I can get it from URL and use it in server side code. And second thing is, the query string parameters are not plain text, they are encrypted with a strong algorithm. So, below is the small function I written which work perfect.

//Get Querystring name value collection
    public static NameValueCollection GetQueryStringCollection(string url)
    {
        string keyValue = string.Empty;
        NameValueCollection collection = new NameValueCollection();
        string[] querystrings = url.Split('&');
        if (querystrings != null && querystrings.Count() > 0)
        {
            for (int i = 0; i < querystrings.Count(); i++)
            {
                string[] pair = querystrings[i].Split('=');
                collection.Add(pair[0].Trim('?'), pair[1]);
            }
        }
        return collection;
    }

So, this function is a static one because, we are calling it from a Webmethod. And this function is expecting url as the parameter. Actually, it is not complete url, it is just the complete querystring url which starts from the character '?'. Now, how to call and use this function? Find below.

NameValueCollection collection = GetQueryStringCollection(HttpContext.Current.Request.UrlReferrer.Query);
        if (collection != null && collection.Count > 0)
        {
            string id = HttpContext.Current.Server.UrlDecode (collection["id"]);
        } 

The above statement returns the querystring value which has the key "id". This way you can get any querystring value, just by passing it's key name to the collection.

HttpContext.Current.Request.UrlReferrer.Query – Which holds the complete querystring url starts from '?'. So, this way you can get the query string data and use it where ever you want in Page Webmethods.

Is this what you are looking for? Or do you have any other ways to get the querystring data? Please post your ideas on it here.

6 comments:

  1. http://snahta.blogspot.com/2009/08/updating-query-string-values.html

    ReplyDelete
  2. Great article! Thanks for taking the time to explain Get Querystring parameters in Page Webmethods ASP.NET. I've been thinking about similar topics lately, and it's good to see that I'm not alone. What do you think about jQuery Validation and ASP.NET MVC Forms?

    ReplyDelete
  3. Very nice! I've been wondering about Get Querystring parameters in Page Webmethods ASP.NET lately. Do you have any thoughts about JavaScriptMVC - Include?

    ReplyDelete
  4. Very nice! I've been wondering about Get Querystring parameters in Page Webmethods ASP.NET lately. Do you have any thoughts about JavaScriptMVC - Include?

    ReplyDelete
  5. Hi Balaji,
    Thanks for your valuable comments. One thing is, I don't believe client side validations, whether it is plain javascript or JQuery. We will use client side validation only for quick update user to fill the required fields or some specific match for Regex. But how is it feasible? What if i disabled javascript in my browser? So, client side validation is a simple part in any programming, but we should do server side validation too for good results. Otherwise, you will be end up with bad data which is simply useless in future. And about ASP.NET MVC forms, you can use them and JQuery works well for that architecture. Still waiting for the solid MVC version, which solves the problems currently it has. Please feel free to ask any questions you have or follow me in my blog.

    -Praveen.

    ReplyDelete
  6. any chance I could get this translated into vb.net?

    ReplyDelete