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

Page History: QueryString Class

Compare Page Revisions



« Older Revision - Back to Page History - Newer Revision »


Page Revision: Wed, Nov 04, 2009, 7:24 AM


This page is part of the Class Library Pages collection.
Click the icon to see the index.
This class depends on the Parser class, found here.

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. Modify the Key enum to suit your requirements
  3. Modify the static constructor to fill the _dict dictionary.

Source Code

VB.NET

{copytext|VbSource}
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.WebControls.WebParts
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic

Public Class QueryString

    Public Enum Key
        RequestId
        AppId
        UrlReferrer
    End Enum

    Private Shared _dict As Dictionary(Of Key, String)

    '== Shared Members ============================================================================
    Shared Sub New()
        _dict = New Dictionary(Of Key, String)()
        _dict.Add(Key.RequestId, "rid")
        _dict.Add(Key.AppId, "aid")
        _dict.Add(Key.UrlReferrer, "url")
    End Sub
    Public Shared Function GetKey(ByVal key As Key) As String
        Return _dict(key)
    End Function
    Public Shared Function GetValue(ByVal key As Key) As String

        Dim s As String = ""

        Dim request As HttpRequest = HttpContext.Current.Request

        If request IsNot Nothing Then
            s = request.QueryString(_dict(key))
            If s Is Nothing Then
                s = ""
            End If
        End If

        Return s

    End Function
    Public Shared Function TryGetValue(Of T)(ByVal key As Key, ByRef value As T) As Boolean

        Return Parser(Of T).TryParse(GetValue(key), value)

    End Function

    '== Instance Members ==========================================================================
    Private _items As Dictionary(Of Key, String)
    Private _pageUrl As String

    Public Sub New(ByVal pageUrl As String)
        _pageUrl = pageUrl
        _items = New Dictionary(Of Key, String)
    End Sub
    Private Sub New(ByVal pageUrl As String, ByVal items As Dictionary(Of Key, String))
        _pageUrl = pageUrl
        _items = items
    End Sub
    Public Sub Add(ByVal key As Key, ByVal value As String)
        _items.Add(key, value)
    End Sub
    Public Function FullUrl() As String
        Return FullUrl(_pageUrl)
    End Function
    Public Function FullUrl(ByVal pageUrl As String) As String

        Dim result As String = pageUrl
        Dim delim As String = "?"

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

        Return result

    End Function
    Public Function Clone() As QueryString

        Dim items As Dictionary(Of Key, String) = New Dictionary(Of Key, String)
        For Each key As Key In _items.Keys
            items.Add(key, _items(key))
        Next
        Return New QueryString(_pageUrl, items)

    End Function

End Class

C#

{copytext|CsSource}
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public class QueryString
{
    public enum Key { Season, EpisodeId, PersonId, Scene, SearchFor, Mode }
    
    private static Dictionary<Key, string> _dict;
    private Dictionary<Key, string> _items;
    private string _pageUrl;

    static QueryString()
    {
        _dict = new Dictionary<Key,string>();
        _dict.Add(Key.EpisodeId, "eid" );
        _dict.Add(Key.Mode,        "m"  );
        _dict.Add(Key.PersonId,  "pid" );
        _dict.Add(Key.Scene,     "scid");
        _dict.Add(Key.SearchFor, "sf"  );
        _dict.Add(Key.Season,    "sid" );
    }
    public static bool TryGetValue<T>(Key key, out T value)
    {
        return Parser<T>.TryParse(GetValue(key), out value);
    }
    public static string GetValue(Key key)
    {
        string s = "";
        HttpRequest request = HttpContext.Current.Request;

        if (request != null)
            s = request.QueryString[_dict[key]];

        return s;
    }
    public static string GetKey(Key key)
    {
        return _dict[key];
    }
    public QueryString(string pageUrl)
    {
        _pageUrl = pageUrl;
        _items = new Dictionary<Key,string>();
    }
    private QueryString(string pageUrl, Dictionary<Key, string> items)
    {
        _pageUrl = pageUrl;
        _items = items;
    }
    public void Add(Key key, string value)
    {
        _items.Add(key, value);
    }
    public string FullUrl()
    {
        return FullUrl(_pageUrl);
    }
    public string FullUrl(string pageUrl)
    {
        string result = pageUrl;
        string delim = "?";

        foreach (QueryString.Key s in _items.Keys)
        {
            result += delim;
            result += GetKey(s);
            result += "=";
            result += HttpContext.Current.Server.UrlEncode(_items[s]);
            delim = "&";
        }
        return result;
    }
    public QueryString Clone()
    {
        try
        {
            Dictionary<Key, string> items = new Dictionary<Key, string>();
            foreach (Key key in _items.Keys)
                try
                {
                    items.Add(key, _items[key]);
                }
                catch (Exception ex)
                {
                    throw ex;
                }

            return new QueryString(_pageUrl, items);
        }
        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.