从请求中添加一些参数字典

时间:2014-04-07 12:34:39

标签: c# asp.net

我想从Request

获得一些参数

我需要从Request.Params所有params文本包含“txt”我有更多类型的文本结构:

"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone2"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtPhone3"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr1"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr2"
"ctl00$cphMain$repDelTypes$ctl00$ucDel$txtAdr3"

如何获取“txt”之后所有文本的值

var dictionary =  new Dictionary<string, string>();

 foreach (var key in Request.Params.AllKeys)
 { 
      if (key.ToString().Contains("txt"))
                {
                 // add to dictionary name and value
                // dictionary.Add("name", "val");
                }
 }

3 个答案:

答案 0 :(得分:2)

你可以这样做:

var dictionary =  new Dictionary<string, string>();
foreach (var key in Request.Params.AllKeys)
{ 
     if (key.ToString().Contains("txt"))
     {
          int index = Request.Params[key].LastIndexOf("txt");
          Dictionary.Add(key, Request.Params[key].SubString(index));
     }
}

答案 1 :(得分:1)

您是否在询问如何添加到词典中?

var dictionary =  new Dictionary<string, string>();
foreach (var key in Request.Params.AllKeys)
{ 
     if (key.ToString().Contains("txt"))
     {
          //get the text after "txt"
          var index = Request.Params[key].LastIndexOf("txt");
          var val = Request.Params[key].SubString(index);
          Dictionary.Add(key, val);
     }
}

答案 2 :(得分:-1)

var dictionary =  new Dictionary<string, string>();

 foreach (var key in Request.Params.AllKeys)
 { 
      if (key.ToString().Contains("txt"))
      {
         // add to dictionary name and value
         dictionary.Add(key.Split(new string[]{"txt"}, StringSplitOptions.None)[1], Request.Params[key]);
      }
 }