A useful urlbuilder class

Source: Internet
Author: User
Code

Using system;
Using system. Web;
Using system. Collections. Specialized;

Namespace craiglabs. Common {
Public class urlbuilder: uribuilder {
Stringdictionary _ querystring = NULL;

# Region Properties
Public stringdictionary querystring {
Get {
If (_ querystring = NULL ){
_ Querystring = new stringdictionary ();
}

Return _ querystring;
}
}

Public String pagename {
Get {
String Path = base. path;
Return Path. substring (path. lastindexof ("/") + 1 );
}
Set {
String Path = base. path;
Path = path. substring (0, path. lastindexof ("/"));
Base. Path = string. Concat (path, "/", value );
}
}
# Endregion
 
# Region constructor overloads
Public urlbuilder (): Base (){
}
 
Public urlbuilder (string URI): Base (URI ){
Populatequerystring ();
}
 
Public urlbuilder (URI): Base (URI ){
Populatequerystring ();
}
 
Public urlbuilder (string schemename, string hostname): Base (schemename, hostname ){
}
 
Public urlbuilder (string scheme, string host, int portnumber): Base (scheme, host, portnumber ){
}
 
Public urlbuilder (string scheme, string host, int port, string pathvalue): Base (scheme, host, port, pathvalue ){
}
 
Public urlbuilder (string scheme, string host, int port, string path, string extravalue): Base (scheme, host, port, path, extravalue ){
}
 
Public urlbuilder (system. Web. UI. Page): Base (page. Request. url. absoluteuri ){
Populatequerystring ();
}
# Endregion
 
# Region Public Methods
Public New String tostring (){
Getquerystring ();
 
Return base. Uri. absoluteuri;
}
 
Public void navigate (){
_ Navigate (true );
}
 
Public void navigate (bool endresponse ){
_ Navigate (endresponse );
}
 
Private void _ navigate (bool endresponse ){
String uri = This. tostring ();
Httpcontext. Current. response. Redirect (Uri, endresponse );
}
# Endregion
 
# Region private methods
Private void populatequerystring (){
String query = base. query;

If (query = string. Empty | query = NULL ){
Return;
}

If (_ querystring = NULL ){
_ Querystring = new stringdictionary ();
}

_ Querystring. Clear ();

Query = query. substring (1); // remove?
 
String [] pairs = query. Split (New char [] {'&'});
Foreach (string s in pairs ){
String [] pair = S. Split (New char [] {'= '});

_ Querystring [pair [0] = (pair. length> 1 )? Pair [1]: String. empty;
}
}
 
Private void getquerystring (){
Int COUNT = _ querystring. count;

If (COUNT = 0 ){
Base. query = string. empty;
Return;
}
 
String [] keys = new string [count];
String [] values = new string [count];
String [] pairs = new string [count];
 
_ Querystring. Keys. copyto (keys, 0 );
_ Querystring. Values. copyto (values, 0 );

For (INT I = 0; I <count; I ++ ){
Pairs [I] = string. Concat (Keys [I], "=", values [I]);
}

Base. query = string. Join ("&", pairs );
}
# Endregion
}
}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.