1
using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Web;
5 using System.Collections.Specialized;
6 using eBriny.Framework.Core;
7
8 namespace eBriny.Framework.Web
9 {
10 /// <summary>
11 /// HttpContext处理类
12 /// </summary>
13 public class HttpContextHelper
14 {
15 private static HttpContextHelper _Instance = new HttpContextHelper();
16
17 private HttpContextHelper()
18 {
19 }
20
21 /// <summary>
22 /// 实例
23 /// </summary>
24 public static HttpContextHelper Instance
25 {
26 get
27 {
28 if (EnvironmentHelper.Mode == EnvironmentHelper.InstanceMode.Windows)
29 {
30 throw new NotSupportedException();
31 }
32 return _Instance;
33 }
34 }
35
36 /// <summary>
37 /// 获取当前HttpContext.Request中参数的值
38 /// </summary>
39 /// <typeparam name="T"> 数据类型 </typeparam>
40 /// <param name="key"> 参数名称 </param>
41 /// <param name="objValue"> 如果值为空或不存在返回的默认值 </param>
42 /// <returns></returns>
43 public T GetRequestParameterValue<T>( string key, T objValue)
44 {
45 return GetRequestParameterValue<T>(key, objValue, false);
46 }
47
48 /// <summary>
49 /// 获取当前HttpContext.Request中参数的值
50 /// </summary>
51 /// <typeparam name="T"> 数据类型 </typeparam>
52 /// <param name="key"> 参数名称 </param>
53 /// <param name="objValue"> 如果值为空或不存在返回的默认值 </param>
54 /// <param name="urlDecode"> 是否需要UrlDecode解码操作 </param>
55 /// <returns></returns>
56 public T GetRequestParameterValue<T>( string key, T objValue, bool urlDecode)
57 {
58 if (! string.IsNullOrEmpty(HttpContext.Current.Request[key]))
59 {
60 return (T)Convert.ChangeType(urlDecode ?
61 HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request[key]) :
62 HttpContext.Current.Request[key], typeof(T));
63 }
64
65 return objValue;
66 }
67
68 /// <summary>
69 /// 将参数重新组合成Url
70 /// </summary>
71 /// <param name="uriString"> url </param>
72 /// <param name="requestParamsArray"> 参数集合的数组 </param>
73 /// <returns> 补充了参数的url </returns>
74 public string CombineUrlParams( string uriString, params NameValueCollection[] requestParamsArray)
75 {
76 return CombineUrlParams(uriString, Encoding.UTF8, requestParamsArray);
77 }
78
79 /// <summary>
80 /// 将参数重新组合成Url
81 /// </summary>
82 /// <param name="uriString"> url </param>
83 /// <param name="encoding"> 字符编码 </param>
84 /// <param name="requestParamsArray"> 参数集合的数组 </param>
85 /// <returns> 补充了参数的url </returns>
86 public string CombineUrlParams( string uriString, Encoding encoding, params NameValueCollection[] requestParamsArray)
87 {
88 if ( string.IsNullOrEmpty(uriString))
89 {
90 throw new ArgumentNullException( " uriString ");
91 }
92 if (encoding == null)
93 {
94 throw new ArgumentNullException( " encoding ");
95 }
96 if (requestParamsArray == null)
97 {
98 throw new ArgumentNullException( " requestParamsArray ");
99 }
100
101 NameValueCollection requestParams = MergeParamsCollection(requestParamsArray);
102
103 StringBuilder strBuilder = new StringBuilder( 1024);
104
105 string leftPart = string.Empty;
106
107 int startIndex = uriString.IndexOf( ' ? ');
108
109 leftPart = (startIndex >= 0) ? uriString.Substring( 0, startIndex) : uriString;
110
111 for ( int i = 0; i < requestParams.Count; i++)
112 {
113 strBuilder.Append(i.Equals( 0) ? " ? " : " & ");
114
115 strBuilder.AppendFormat( " {0}={1} ",
116 HttpUtility.UrlEncode(requestParams.Keys[i], encoding),
117 HttpUtility.UrlEncode(requestParams[i], encoding));
118 }
119
120 return string.Format( " {0}{1} ", leftPart, strBuilder.ToString());
121 }
122
123 /// <summary>
124 /// 得到URL锚点的信息。"#"后面的部分
125 /// </summary>
126 /// <param name="queryString"> 请求的字符串( http://localhost/lianhome #littleTurtle) </param>
127 /// <returns></returns>
128 public string GetAnchorPointStringInUrl( string queryString)
129 {
130 if ( string.IsNullOrEmpty(queryString))
131 {
132 throw new ArgumentNullException( " queryString ");
133 }
134
135 int anchorPointStart = - 1;
136
137 for ( int i = queryString.Length - 1; i >= 0; i--)
138 {
139 if (queryString[i].Equals( ' # '))
140 {
141 anchorPointStart = i;
142 }
143 else
144 {
145 if (queryString[i].Equals( ' & ') || queryString[i].Equals( ' ? '))
146 {
147 break;
148 }
149 }
150 }
151
152 string result = string.Empty;
153
154 if (anchorPointStart >= 0)
155 {
156 result = queryString.Substring(anchorPointStart);
157 }
158
159 return result;
160 }
161
162 #region 私有方法
163 private NameValueCollection MergeParamsCollection(NameValueCollection[] requestParams)
164 {
165 NameValueCollection result = new NameValueCollection();
166
167 for ( int i = 0; i < requestParams.Length; i++)
168 MergeTwoParamsCollection(result, requestParams[i]);
169
170 return result;
171 }
172
173 private void MergeTwoParamsCollection(NameValueCollection target, NameValueCollection src)
174 {
175 foreach ( string key in src.Keys)
176 {
177 if (target[key] == null)
178 target.Add(key, src[key]);
179 }
180 }
181 #endregion
182 }
183 }
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Web;
5 using System.Collections.Specialized;
6 using eBriny.Framework.Core;
7
8 namespace eBriny.Framework.Web
9 {
10 /// <summary>
11 /// HttpContext处理类
12 /// </summary>
13 public class HttpContextHelper
14 {
15 private static HttpContextHelper _Instance = new HttpContextHelper();
16
17 private HttpContextHelper()
18 {
19 }
20
21 /// <summary>
22 /// 实例
23 /// </summary>
24 public static HttpContextHelper Instance
25 {
26 get
27 {
28 if (EnvironmentHelper.Mode == EnvironmentHelper.InstanceMode.Windows)
29 {
30 throw new NotSupportedException();
31 }
32 return _Instance;
33 }
34 }
35
36 /// <summary>
37 /// 获取当前HttpContext.Request中参数的值
38 /// </summary>
39 /// <typeparam name="T"> 数据类型 </typeparam>
40 /// <param name="key"> 参数名称 </param>
41 /// <param name="objValue"> 如果值为空或不存在返回的默认值 </param>
42 /// <returns></returns>
43 public T GetRequestParameterValue<T>( string key, T objValue)
44 {
45 return GetRequestParameterValue<T>(key, objValue, false);
46 }
47
48 /// <summary>
49 /// 获取当前HttpContext.Request中参数的值
50 /// </summary>
51 /// <typeparam name="T"> 数据类型 </typeparam>
52 /// <param name="key"> 参数名称 </param>
53 /// <param name="objValue"> 如果值为空或不存在返回的默认值 </param>
54 /// <param name="urlDecode"> 是否需要UrlDecode解码操作 </param>
55 /// <returns></returns>
56 public T GetRequestParameterValue<T>( string key, T objValue, bool urlDecode)
57 {
58 if (! string.IsNullOrEmpty(HttpContext.Current.Request[key]))
59 {
60 return (T)Convert.ChangeType(urlDecode ?
61 HttpContext.Current.Server.UrlDecode(HttpContext.Current.Request[key]) :
62 HttpContext.Current.Request[key], typeof(T));
63 }
64
65 return objValue;
66 }
67
68 /// <summary>
69 /// 将参数重新组合成Url
70 /// </summary>
71 /// <param name="uriString"> url </param>
72 /// <param name="requestParamsArray"> 参数集合的数组 </param>
73 /// <returns> 补充了参数的url </returns>
74 public string CombineUrlParams( string uriString, params NameValueCollection[] requestParamsArray)
75 {
76 return CombineUrlParams(uriString, Encoding.UTF8, requestParamsArray);
77 }
78
79 /// <summary>
80 /// 将参数重新组合成Url
81 /// </summary>
82 /// <param name="uriString"> url </param>
83 /// <param name="encoding"> 字符编码 </param>
84 /// <param name="requestParamsArray"> 参数集合的数组 </param>
85 /// <returns> 补充了参数的url </returns>
86 public string CombineUrlParams( string uriString, Encoding encoding, params NameValueCollection[] requestParamsArray)
87 {
88 if ( string.IsNullOrEmpty(uriString))
89 {
90 throw new ArgumentNullException( " uriString ");
91 }
92 if (encoding == null)
93 {
94 throw new ArgumentNullException( " encoding ");
95 }
96 if (requestParamsArray == null)
97 {
98 throw new ArgumentNullException( " requestParamsArray ");
99 }
100
101 NameValueCollection requestParams = MergeParamsCollection(requestParamsArray);
102
103 StringBuilder strBuilder = new StringBuilder( 1024);
104
105 string leftPart = string.Empty;
106
107 int startIndex = uriString.IndexOf( ' ? ');
108
109 leftPart = (startIndex >= 0) ? uriString.Substring( 0, startIndex) : uriString;
110
111 for ( int i = 0; i < requestParams.Count; i++)
112 {
113 strBuilder.Append(i.Equals( 0) ? " ? " : " & ");
114
115 strBuilder.AppendFormat( " {0}={1} ",
116 HttpUtility.UrlEncode(requestParams.Keys[i], encoding),
117 HttpUtility.UrlEncode(requestParams[i], encoding));
118 }
119
120 return string.Format( " {0}{1} ", leftPart, strBuilder.ToString());
121 }
122
123 /// <summary>
124 /// 得到URL锚点的信息。"#"后面的部分
125 /// </summary>
126 /// <param name="queryString"> 请求的字符串( http://localhost/lianhome #littleTurtle) </param>
127 /// <returns></returns>
128 public string GetAnchorPointStringInUrl( string queryString)
129 {
130 if ( string.IsNullOrEmpty(queryString))
131 {
132 throw new ArgumentNullException( " queryString ");
133 }
134
135 int anchorPointStart = - 1;
136
137 for ( int i = queryString.Length - 1; i >= 0; i--)
138 {
139 if (queryString[i].Equals( ' # '))
140 {
141 anchorPointStart = i;
142 }
143 else
144 {
145 if (queryString[i].Equals( ' & ') || queryString[i].Equals( ' ? '))
146 {
147 break;
148 }
149 }
150 }
151
152 string result = string.Empty;
153
154 if (anchorPointStart >= 0)
155 {
156 result = queryString.Substring(anchorPointStart);
157 }
158
159 return result;
160 }
161
162 #region 私有方法
163 private NameValueCollection MergeParamsCollection(NameValueCollection[] requestParams)
164 {
165 NameValueCollection result = new NameValueCollection();
166
167 for ( int i = 0; i < requestParams.Length; i++)
168 MergeTwoParamsCollection(result, requestParams[i]);
169
170 return result;
171 }
172
173 private void MergeTwoParamsCollection(NameValueCollection target, NameValueCollection src)
174 {
175 foreach ( string key in src.Keys)
176 {
177 if (target[key] == null)
178 target.Add(key, src[key]);
179 }
180 }
181 #endregion
182 }
183 }