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
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;
}
}
}