一个有用的UrlBuilder类
代码
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 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 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 the ?
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
}
}
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 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 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 the ?
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
}
}
(全文完)
以下为广告部分
您部署的HTTPS网站安全吗?
如果您想看下您的网站HTTPS部署的是否安全,花1分钟时间来 myssl.com 检测以下吧。让您的HTTPS网站变得更安全!
快速了解HTTPS网站安全情况。
安全评级(A+、A、A-...)、行业合规检测、证书信息查看、证书链信息以及补完、服务器套件信息、证书兼容性检测等。
安装部署SSL证书变得更方便。
SSL证书内容查看、SSL证书格式转换、CSR在线生成、SSL私钥加解密、CAA检测等。
让服务器远离SSL证书漏洞侵扰
TLS ROBOT漏洞检测、心血漏洞检测、FREAK Attack漏洞检测、SSL Poodle漏洞检测、CCS注入漏洞检测。
作者:朱祁林
出处:http://zhuqil.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。