如何识别该用户在url中多次发送相同的查询字符串

时间:2017-08-10 04:27:19

标签: asp.net

这是我的示例网站页面。我将这个网址(Api)提供给其他人:

http://localhost:someport/Test.aspx?name=pinky&SId=123&PhoneNo=XXXX

在这个页面中,我正在访问像这样的查询字符串

if (Request.QueryString["name"] != null)
{
   Name = Request.QueryString["name"].ToString();                               
}

其他人可以从他们的软件发出请求/调用我的页面。有时他们可以传递相同的查询字符串两次以上。就像这个

http://localhost:someport/Test.aspx?name=pinky&SId=123&PhoneNo=XXXX&SId=43

在我的页面

String SId = "";
 if (Request.QueryString["SId"] != null)
    {
       SId= Request.QueryString["SId"].ToString();                               
    }

SId= "123,43";

如果SId像这样整个我的逻辑改变/失败。我不想这样。如果 相同的查询字符串不止一个,应该显示消息。

  1. 如何识别查询字符串是否多次出现
  2. 如果用户通过name=pinky&rani,我的网页只会选择小指
  3. 如何处理两者?

2 个答案:

答案 0 :(得分:0)

为什么你不使用String.Split

试试这个:

if (Request.QueryString["SId"] != null)
{
   var SIds = Request.QueryString["SId"].ToString().Split(',');  
   if(SIds.Count()>1)  
      ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('SIds come more than one!')", true);
   else 
      SId = SIds[0];
}

答案 1 :(得分:0)

在下面的代码的帮助下,我能够找出传递相同查询字符串的时间

string productID = null;
            foreach (string productID_loopVariable in Request.QueryString.GetValues("Password"))
            {
                productID = productID_loopVariable;
                // do something with productID
            }
相关问题