【问题标题】:Can't decode cyrillic value from Request.QueryString无法从 Request.QueryString 解码西里尔文值
【发布时间】:2013-06-04 15:50:52
【问题描述】:

在我的 IIS7 上,我有 ASP.NET WebForms 站点,我在查询字符串中使用西里尔字母值。我在重定向时使用HttpUtility.UrlEncode 作为参数,最后我有如下网址:

http://mysite.com/Search.aspx?SearchText=текст

当我尝试读取参数 SearchText 值(包括 HttpUtility.Decode() 函数)时,它返回错误的值 ÑекÑÑ,但应该返回 текст

它在 ASP.NET 开发服务器上的本地主机上工作,但在 IIS7 上不起作用(包括本地 IIS7) 在我的 web.config 中,我设置了行

<globalization requestEncoding="utf-8" responseEncoding="utf-8" />

但它仍然不起作用。

感谢任何帮助,

非常感谢!

【问题讨论】:

    标签: c# asp.net iis-7 webforms


    【解决方案1】:

    问题实际上出在我在 Web 应用程序中使用的 UrlRewriting.net 中。

    【讨论】:

    • 然后选择你的作为答案,我将删除我的。
    • 我有一个类似的问题,使用不同的应用程序。你能帮我吗?我看到 %u00d1%u0081%u00d1%u008b%u00d1%u0080%u00d1%u008c%u00d0%u00b5%u00d0%u00b2%u00d1%u008b%u00d0%u00b5-%u00d1%u0082%u00d0%u00bjj%u u00d0%u00b0%u00d1%u0080%u00d1%u008b 在查询字符串中,它被解码为ÑÑÑÑевÑе-ÑовjjjаÑÑ
    【解决方案2】:

    我通过将值转换为 ToBase64String 解决了同样的问题:

    1. 在重定向到目标页面之前,我对值进行了编码:

          Dim Data() As Byte  'For the data to  be encoded
      
          'Convert the string into a byte array
          Dim encoding As New System.Text.UTF8Encoding
          Data = encoding.GetBytes(ParamToPass)
      
          'Converting to ToBase64String
          Dim EncodedStringToPass as string = Convert.ToBase64String(Data)
      
          Page.Response.Redirect("TargetPage.aspx?Param=" & EncodedStringToPass, False)
      
    2. 在目标页面:

          Dim Data() As Byte  'For the data to  be decoded
          Data = Convert.FromBase64String(Page.Request.Params("Param"))
      
          Dim encoding As New System.Text.UTF8Encoding
          Dim ParamToPass As String = encoding.GetString(Data)
      

    附:该方法唯一的缺点是无法看到浏览器的url字符串中参数的真实值。但就我而言,这没有问题

    【讨论】:

      【解决方案3】:

      如果你使用redirect函数,是的,里面有这个调用

      url = UrlEncodeRedirect(url);

      这打破了西里尔语、希腊语和其他可能的字符。如果我没记错的话(我说记住是因为这个问题来自我几个月前的经验)字符的中断是在? 符号之后。无论如何,我有同样的问题。

      可能的解决方案:

      • 进行自定义重定向,可能不如原来的那么好,但可以绕过这个问题。
      • 为您的重定向逻辑寻找一些替代方法。
      • 使您的自定义文本编码仅使用重定向不会更改的有效 url 字符,然后再次解码它们。不利的是,它会像隐藏文本而不是可见的可读搜索词。

      这是重定向的基础。

      public static void RedirectSimple(string url, bool endResponse)
      {
          HttpResponse MyResponse = HttpContext.Current.Response;
      
          MyResponse.Clear();
          MyResponse.TrySkipIisCustomErrors = true;
          MyResponse.StatusCode = 302;
          MyResponse.Status = "302 Temporarily Moved";
          MyResponse.RedirectLocation = url;
      
          MyResponse.Write("<html><head><title>Object moved</title></head><body>\r\n");
          MyResponse.Write("<h2>Object moved to <a href=\"" + url + "\">here</a>.</h2>\r\n");
          MyResponse.Write("</body></html>\r\n");
      
          if (endResponse){
              MyResponse.End();
          }
      }
      

      你可以把它做成一个函数,试试看是否能正常工作。

      【讨论】:

      • 这是经验之谈,我也遇到了同样的问题,分析了一下,搜了这么久才发现。当然我可能犯了一些错误并且有替代解决方案 - 但目前我已经根据 asp.net 重定向重写了我的一个重定向。
      • 嗯,很奇怪为什么这在本地 ASP.NET 开发服务器上工作,也忘了说它在 IIS6(windows 2003 服务器)上工作成功
      • @ihorko 我没有所有答案 - 我使用 IIS7 和本地 IIS5 进行测试,我曾经在重定向方面遇到同样的问题。
      猜你喜欢
      • 2019-07-24
      • 1970-01-01
      • 2015-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多