首页 > 代码库 > Asp.net 插入或更改查询字符串

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                 else10                 {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 =http://www.mamicode.com/ Request.QueryString[k];23                             keyCurrent = k;24                         }25                         else26                         {27                             keyValue =http://www.mamicode.com/ 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                         else41                         {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         }