2011-07-01 46 views
1

我有一个移动应用程序,有几个文件夹。这些文件夹作为变量,我将增加参数:从URL手机,重定向将参数%20添加到参数

 //Get the URL 
     string url = HttpContext.Current.Request.Url.AbsolutePath; 

     //Location 
     string location = ""; 

     //Check if string contains/
     if (url.Contains('/')) 
     { 
      //Get location 
      string[] words = url.Split('/'); 

      //Set location 
      location = words[1]; 

      //Now check if string contains ? 
      if (location.Contains('?')) 
      { 
       //Remove ? 
       string[] removeQ = location.Split('?'); 

       //Reset location 
       location = removeQ[0]; 
      } 

     } 

我以后的道路我根据手机类型重定向:

 if (Request.UserAgent.ToUpper().Contains("BLACKBERRY")) 
     { 
      //Now we check the version 
      if (double.Parse(Request.Browser.Version) <= 5) 
      { 
       Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
      } 
      else if (double.Parse(Request.Browser.Version) >= 6) 
      { 
       Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
      } 
      else 
      { 
       Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
      } 

     } 
     else if (Request.UserAgent.ToUpper().Contains("HTC")) //HTC phones cannot handle JQuery mobile 
     { 
      Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
     } 
     else if (Request.UserAgent.ToUpper().Contains("ANDROID")) //Certain androids are out of date 
     { 
      if (double.Parse(Request.Browser.Version) > 2) 
      { 
       Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
      } 
      else 
      { 
       Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
      } 

     } 
     else 
     { 
      Response.Redirect("http://myWebsite.com/Default.aspx?location= " + location); 
     } 
    } 

的问题是,它location将在前面添加“%20”。这不会发生,如果我在台式机上测试它,只能在手机上测试。

这是怎么发生的?我怎样才能防止这一点?

回答

2

删除字符串文字末尾的所有空格,例如改变:

"http://myWebsite.com/Default.aspx?location= " + location 

"http://myWebsite.com/Default.aspx?location=" + location 
+0

啊。愚蠢的错误。虽然有好点子! –

0

在URL,空格被编码以%20。您只需删除字符串中的任何尾随或前导空格即可修改该字符串。