Jasinski Technical Wiki

Navigation

Home Page
Index
All Pages

Quick Search
»
Advanced Search »

Contributor Links

Create a new Page
Administration
File Management
Login/Logout
Your Profile

Other Wiki Sections

Software

PoweredBy

QueryString Class

RSS
Modified on Mon, Nov 23, 2009, 10:18 AM by Administrator Categorized as ASP·NET Web Forms, Class Library
This page is part of the Class Library Pages collection.
Click the icon to see the index.

Overview

The QueryString class provides a means of working with query strings in any ASP.NET website.

Implementation

  1. Copy the source code, found below
  2. Be sure your project contains the QueryStringLookup and Lookup classes, found here. (This class depends on the QueryStringLookup class, which is derived from the Lookup class.)
  3. Modify the QueryStringKey enum to suit your requirements

Source Code

VB.NET

{copytext|VbSource}
Imports System
Imports System.Collections.Generic

Public Class QueryString

    Private _items As Dictionary(Of QueryStringKey, String)
    Private _pageUrl As String
    Private _requestQs As NameValueCollection
    '----------------------------------------------------------------------------------------------
    Public Sub New(ByVal pageUrl As String, Optional ByVal requestQs As NameValueCollection = _
    Nothing)
        _pageUrl = pageUrl
        _items = New Dictionary(Of QueryStringKey, String)
        _requestQs = requestQs
    End Sub
    '----------------------------------------------------------------------------------------------
    Private Sub New(ByVal pageUrl As String, ByVal items As Dictionary(Of QueryStringKey, _
    String), ByVal requestQs As NameValueCollection)
        _pageUrl = pageUrl
        _items = items
        _requestQs = requestQs
    End Sub
    '----------------------------------------------------------------------------------------------
    Public Function Add(ByVal key As QueryStringKey, ByVal value As String) As QueryString
        _items.Add(key, value)
        Return Me
    End Function
    '----------------------------------------------------------------------------------------------
    Public Function Remove(ByVal key As QueryStringKey) As QueryString
        _items.Remove(key)
        Return Me
    End Function
    '----------------------------------------------------------------------------------------------
    Public Function FullUrl(Optional ByVal requestQs As NameValueCollection = Nothing) As String
        Return FullUrl(_pageUrl, requestQs)
    End Function
    '----------------------------------------------------------------------------------------------
    Public Function FullUrl(ByVal pageUrl As String, Optional ByVal requestQs As _
    NameValueCollection = Nothing) As String

        If requestQs IsNot Nothing Then
            _requestQs = requestQs
        End If

        Dim result As String = pageUrl
        Dim delim As String = "?"
        Dim qsl As QueryStringLookup = New QueryStringLookup()

        For Each s As QueryStringKey In _items.Keys
            result &= delim
            result &= qsl.GetKey(s)
            result &= "="
            result &= HttpContext.Current.Server.UrlEncode(_items(s))
            delim = "&"
        Next

        If _requestQs IsNot Nothing Then

            For Each s As String In _requestQs.Keys

                result &= delim
                result &= s
                result &= "="
                result &= _requestQs(s)
                delim = "&"

            Next

        End If

        Return result

    End Function
    '----------------------------------------------------------------------------------------------
    Public Function Clone() As QueryString

        Dim items As Dictionary(Of QueryStringKey, String) = _
                    New Dictionary(Of QueryStringKey, String)

        For Each key As QueryStringKey In _items.Keys
            items.Add(key, _items(key))
        Next

        Dim requestQs As NameValueCollection = New NameValueCollection()

        For Each s As String In _requestQs
            requestQs.Add(s, _requestQs(s))
        Next

        Return New QueryString(_pageUrl, items, requestQs)

    End Function

End Class

C#

{copytext|CsSource}
using System;
using System.Collections.Generic;

public class QueryString
{
    
    private Dictionary<QueryStringKey, string> _items;
    private string _pageUrl;
    private NameValueCollection _requestQs;

    //---------------------------------------------------------------------------------------------
    public QueryString(string pageUrl)
    {
        _pageUrl = pageUrl;
        _items = new Dictionary<QueryStringKey, string>();
        _requestQs = null;
    }
    //---------------------------------------------------------------------------------------------
    public QueryString(string pageUrl, NameValueCollection requestQs)
    {
        _pageUrl = pageUrl;
        _items = new Dictionary<QueryStringKey, string>();
        _requestQs = requestQs
    }
    //---------------------------------------------------------------------------------------------
    private QueryString(string pageUrl, Dictionary<QueryStringKey, string> items, 
    NameValueCollection requestQs)
    {
        _pageUrl = pageUrl;
        _items = items;
        _requestQs = requestQs;
    }
    //---------------------------------------------------------------------------------------------
    public QueryString Add(QueryStringKey key, string value)
    {
        _items.Add(key, value);
        return this;
    }
    //---------------------------------------------------------------------------------------------
    public QueryString Remove(QueryStringKey key)
    {
        _items.Remove(key);
        return this;
    }
    //---------------------------------------------------------------------------------------------
    public string FullUrl()
    {
        return FullUrl(_pageUrl);
    }
    //---------------------------------------------------------------------------------------------
    public string FullUrl(string pageUrl)
    {
        return FullUrl(pageUrl, null);
    }
    //---------------------------------------------------------------------------------------------
    public string FullUrl(string pageUrl, NameValueCollection requestQs)
    {
        if (requestQs != null)
            _requestQs = requestQs

        string result = pageUrl;
        string delim = "?";
        QueryStringLookup qsl = new QueryStringLookup();

        foreach (QueryStringKey s in _items.Keys)
        {
            result += delim;
            result += qs.GetKey(s);
            result += "=";
            result += HttpContext.Current.Server.UrlEncode(_items[s]);
            delim = "&";
        }

        if (_requestQs != null)
        {
            foreach (string s in _requestQs.Keys)
            {
                result += delim;
                result += s;
                result += "=";
                result += _requestQs[s];
                delim = "&";
            }
        }

        return result;
    }
    //---------------------------------------------------------------------------------------------
    public QueryString Clone()
    {
        try
        {
            Dictionary<QueryStringKey, string> items = new Dictionary<QueryStringKey, string>();
            foreach (QueryStringKey key in _items.Keys)
                try
                {
                    items.Add(key, _items[key]);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            NameValueCollection requestQs = new NameValueCollection();
            foreach (string s in requestQs)
                requestQs.Add(s, requestQs[s]);

            return new QueryString(_pageUrl, items, requestQs);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}

ScrewTurn Wiki version 3.0.1.400. Some of the icons created by FamFamFam. Except where noted, all contents Copyright © 1999-2024, Patrick Jasinski.