Server 端接收 Request.Form 表单资料后依栏位名称排序

// 方法1:直接以 NameValueCollection 的 Key 排序
// 但此方法无法移除表单中不要的值
string formValue = string.Empty;
foreach (string key in nvc.AllKeys.OrderBy(keyname => keyname))
{
formValue += key + " = " + nvc[key] + "<br>";
}

// 方法2:先把 NameValueCollection 转为 Dictionary,再以 Dictionary 的 Key 值排序后再转成 Dictionary
Dictionary<string, string> formDict = nvc.AllKeys.ToDictionary(keyname => keyname, keyname => nvc[keyname]);
// 把 Dictionary 的内容依照 Key 值排序
Dictionary<string, string> formDictOrder = formDict.OrderBy(data => data.Key).ToDictionary(keydata => keydata.Key, keydata => keydata.Value);

// 方法3:先以 NameValueCollection 的 Key 值排序后再转成 Dictionary
Dictionary<string, string> formDictOrder2 = nvc.AllKeys.OrderBy(keyname => keyname).ToDictionary(keyname => keyname, keyname => nvc[keyname]);
formDictOrder2.Remove("CheckMacValue"); // 移除表单中不要的值

参与评论