ASP.Net QueryString Builder

March 25th, 2008 admin Posted in asp.net, c#, development | No Comments »

I was trying to find any classes allowing easily modify querystring. I have found a couple of good ones but not ideal for me. So I have created my own class. It has a number of static functions which could be called without creating an instance of the class - if you need to use some basic stuff. Here it is:

 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Specialized;
 
namespace ClassLibrary
{
    public class QueryStringBuilder
    {
        protected NameValueCollection m_QueryString;
 
        public QueryStringBuilder(NameValueCollection nvcQueryString)
        {
            m_QueryString = new NameValueCollection(nvcQueryString);
        }
 
        // static function returning a new querystring with a new parameter
        public static NameValueCollection QSWithParameter(NameValueCollection nvcCurrentQS,
                                                      string strParameterName, string strParameterValue)
        {
            NameValueCollection nvcQS = new NameValueCollection(nvcCurrentQS);
 
            for (int i = 0; i < nvcQS.Count; i++)
            {
                if (nvcQS.GetKey(i) == null)
                {
                    nvcQS.Remove(null);
                    break;
                }
            }
            if (nvcQS.GetValues(strParameterName) == null)
                nvcQS.Add(strParameterName, strParameterValue);
            else
                nvcQS[strParameterName] = strParameterValue;
            return nvcQS;
        }
 
        // function returning a new querystring with a new parameter
        public NameValueCollection QSWithParameter(string strParameterName, string strParameterValue)
        {
            return QSWithParameter(m_QueryString, strParameterName, strParameterValue);
        }
 
        // static function returning a querystring without a parameter
        public static NameValueCollection QSWithoutParameter(NameValueCollection nvcCurrentQS,
                                                                               string strParameterName)
        {
            NameValueCollection nvcQS = new NameValueCollection(nvcCurrentQS);
            if (nvcQS.GetValues(strParameterName) != null)
                nvcQS.Remove(strParameterName);
            return nvcQS;
        }
 
        // function returning a querystring without a parameter
        public NameValueCollection QSWithoutParameter(string strParameterName)
        {
            return QSWithoutParameter(m_QueryString, strParameterName);
        }
 
        // static function returning url with a querystring
        public static string UrlWithQueryString(NameValueCollection nvcQueryString, string strUrl)
        {
            int nQS = strUrl.IndexOf("?");
            if (nQS > -1)
                strUrl = strUrl.Substring(0, nQS);
            if (nvcQueryString.Count > 0)
            {
                for (int i = 0; i < nvcQueryString.Count; i++)
                {
                    strUrl += String.Format("{0}{1}={2}", i == 0 ? "?" : "&", nvcQueryString.Keys[i],
                                                                                    nvcQueryString[i]);
                }
            }
            return strUrl;
        }
 
        // static function returning RawUrl with a querystring
        public static string UrlWithQueryString(NameValueCollection nvcQueryString)
        {
            return UrlWithQueryString(nvcQueryString, HttpContext.Current.Request.RawUrl);
        }
 
        // function returning url with a current querystring
        public string UrlWithQueryString(string strUrl)
        {
            return UrlWithQueryString(m_QueryString, strUrl);
        }
 
        // function returning RawUrl with a current querystring
        public string UrlWithQueryString()
        {
            return UrlWithQueryString(m_QueryString);
        }
 
        // procedure removing a parameter from a current querystring
        public void RemoveQSParameter(string strParameterName)
        {
            m_QueryString = QSWithoutParameter(strParameterName);
        }
 
        // procedure adding a parameter to a current querystring
        public void SetQSParameter(string strParameterName, string strParameterValue)
        {
            m_QueryString = QSWithParameter(strParameterName, strParameterValue);
        }
    }

Leave a Reply

You must be logged in to post a comment.