Asp.net 插入或更改查询字符串

 1  string InsertOrUpdateQueryStringItem(string key, string value)
 2         {
 3             if (Request.QueryString.HasKeys())
 4             {
 5                 if (!Array.Exists<string>(Request.QueryString.AllKeys, (qs) => { return qs.Equals(key); }))
 6                 {
 7                     return string.Format("{0}&{1}={2}", Request.RawUrl, key, value);
 8                 }
 9                 else
10                 {
11                     System.Text.StringBuilder pathBuilder = new System.Text.StringBuilder(Request.Path);
12                     pathBuilder.Append("?");
13                     string keyValue;
14                     string keyCurrent;
15                     int handedCount = 0;
16 
17                     foreach (var k in Request.QueryString.AllKeys)
18                     {
19 
20                         if (!k.Equals(key))
21                         {
22                             keyValue = Request.QueryString[k];
23                             keyCurrent = k;
24                         }
25                         else
26                         {
27                             keyValue = value;
28                             keyCurrent = key;
29                         }
30 
31                         if (string.IsNullOrEmpty(keyValue))
32                         {
33                             continue;
34                         }
35 
36                         if (handedCount++ != 0)
37                         {
38                             pathBuilder.AppendFormat("&{0}={1}", keyCurrent, keyValue);
39                         }
40                         else
41                         {
42                             pathBuilder.AppendFormat("{0}={1}", keyCurrent, keyValue);
43                         }
44                     }
45                     return pathBuilder.ToString();
46                 }
47             }
48             return string.Format("{0}?{1}={2}", Request.Path, key, value);
49         }

 

Asp.net 插入或更改查询字符串,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。