【问题标题】:Query strings with special characters查询带有特殊字符的字符串
【发布时间】:2012-03-23 15:54:38
【问题描述】:

如何传递带有特殊字符的查询字符串?

例如,我需要在查询字符串中传递“&”,如下所示:

../solrresults.asp?mode=search&data=M & S

【问题讨论】:

  • 你试过HttpUtility.UrlEncode()
  • 对于经典的ASP,可以使用Server.UrlEncode()函数...
  • Server.UrlEncode() 适用于大多数特殊字符。对于“%”,它不起作用

标签: url asp-classic query-string encode


【解决方案1】:

使用Server.UrlEncode:

URLEncode 转换字符如下: 空格 ( ) 转换为加号 (+)。 非字母数字字符转义为十六进制表示。

这样使用;

<a href="page2.asp?name=<%= Server.URLEncode(sName) %>">here</a>

【讨论】:

  • 我的代码是经典的 ASP,所以我想看看 ASP 本身的方法
【解决方案2】:

在您的 cmets 之后,这里是各种脚本语言的解决方案:

每种脚本语言都有不同的版本。以下是来自 w3schools 的参考资料:

在 JavaScript 中,您可以使用 encodeURI() 函数。 PHP 有 rawurlencode() 函数,ASP 有 Server.URLEncode() 函数。

参考:http://www.w3schools.com/tags/ref_urlencode.asp

【讨论】:

  • 我的代码是经典的 ASP,所以我想看看 ASP 本身的方法
【解决方案3】:

如前所述,UrlEncode method 将是可行的方法。

前段时间我写了a small class QueryString to simplify the work with those query strings

一个示例用法是:

private void Page_Load(
    object sender, 
    System.EventArgs e )
{
    // Let the object fill itself 
    // with the parameters of the current page.
    QueryString qs = new QueryString();

    // Read a parameter from the QueryString object.
    string value1 = qs["name1"];

    // Now remove the parameter.
    qs.RemoveParameter( "name1" );

    // This has the same effect as RemoveParameter() method:
    qs["name1"] = null;

    // ... Further processing of the value1 variable ... 
}

您可以填写查询字符串类,而不必关心是否对值进行 URL 编码,该类会在内部为您处理。

【讨论】:

  • 我的代码是经典的 ASP,所以我想看看 ASP 本身的方法
猜你喜欢
  • 2020-03-18
  • 2018-05-15
  • 1970-01-01
  • 2016-04-22
  • 2012-06-19
  • 2017-10-06
  • 1970-01-01
  • 2011-10-08
  • 1970-01-01
相关资源
最近更新 更多