/*
*
* UrlQuery Class Copyright (c) 2006/2007 Sam Collett (http://www.texotela.co.uk)
* Licensed under the MIT License:
* http://www.opensource.org/licenses/mit-license.php
*
*/
using System;
using System.Web;
using System.Collections.Specialized;
namespace TexoTela
{
///
/// Class for working with Url's
///
public class UrlQuery
{
///
/// Base on current page
///
public UrlQuery()
{
this.url = HttpContext.Current.Request.Url.AbsolutePath;
}
///
/// Base on other page
///
/// The url of the page to reference, i.e.: '/path/to/folder/page.aspx?param1=1¶m2=2'
public UrlQuery(string value)
{
int q = value.IndexOf('?');
if (q != -1)
{
this.url = value.Substring(0, q);
this.queryString = NameValueCollection(value);
}
else
{
this.url = value;
}
}
///
/// Get and set Url parameters
///
public string this[string param]
{
get
{
return this.Get(param);
}
set
{
this.Set(param,value);
}
}
private string url;
///
/// The Url of the page, without QueryString
///
/// /path/to/folder/page.aspx
public string Url
{
get
{
return this.url;
}
}
///
/// Returns the virtual folder the page is in
///
/// /path/to/folder/
public string VirtualFolder
{
get
{
return this.Url.Substring(0, Url.LastIndexOf("/") + 1);
}
}
///
/// The AbsoluteUri
///
/// page.aspx?param1=1¶m2=2
public string AbsoluteUri
{
get
{
return this.Url + this.Get();
}
}
private NameValueCollection queryString;
///
/// Get the QueryString for the page
///
public NameValueCollection QueryString
{
get
{
if (this.queryString != null)
{
return this.queryString;
}
else
{
this.queryString = new NameValueCollection(HttpContext.Current.Request.QueryString);
return this.queryString;
}
}
}
///
/// Get the QueryString
///
/// String in the format ?param1=1¶m2=2
public string Get()
{
string query = "";
if (this.QueryString.Count != 0)
{
query = "?";
for (int i = 0; i <= this.QueryString.Count - 1; i++)
{
if (i != 0)
{
query += "&";
}
query += this.QueryString.GetKey(i) + "=" + this.QueryString.Get(i);
}
}
return query;
}
///
/// Get parameter from QueryString
///
/// Parameter to get
/// Parameter Value
public string Get(string param)
{
return this.QueryString[param];
}
///
/// Set QueryString parameter
///
/// Parameter to set
/// Value of parameter
public void Set(string param, string value)
{
if (param != string.Empty)
{
if (value == string.Empty || value == null)
{
this.QueryString.Remove(param);
}
else
{
this.QueryString[param] = value;
}
}
}
///
/// Use this method to add a NameValueCollection object to the parent object.
/// Validation is done to ensure the Key does not already exist in the QueryString object.
///
///
public void Set(NameValueCollection collection)
{
foreach(string key in collection.Keys)
{
if(this.QueryString[key] != null)
{
this.QueryString.Remove(key);
}
this.Set(key,collection[key]);
}
}
///
/// Convert QueryString string to NameValueCollection
/// http://groups.google.co.uk/group/microsoft.public.dotnet.framework.aspnet/msg/fc3d35bc742f618b?hl=en&lr=&ie=UTF-8&safe=off
///
public static NameValueCollection NameValueCollection(string qs)
{
NameValueCollection nvc = new NameValueCollection();
//strip string data before the question mark
qs = qs.IndexOf('?') > 0 ? qs.Remove(0, qs.IndexOf('?') + 1) : qs;
Array sqarr = qs.Split("&".ToCharArray());
for (int i = 0; i < sqarr.Length; i++)
{
string[] pairs = sqarr.GetValue(i).ToString().Split("=".ToCharArray());
nvc.Add(pairs[0], pairs[1]);
}
return nvc;
}
///
/// Copies a form paramater to the QueryString
///
/// Form Parameter
public void FormToQuery(string param)
{
this.Set(param, HttpContext.Current.Request.Form[param]);
}
}
}