回发后的编码损坏 [英] Broken encoding after postback

查看:157
本文介绍了回发后的编码损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个查询字符串,其参数值包含挪威字符å编码为%e5 。页面包含一个具有由ASP.Net自动填充的操作属性的表单。当URL被输出到所述属性时,它被打印一个完整的两字节编码:%u00e5

I have a query string with a parameter value that contains the norwegian character å encoded as %e5. The page contains a form with an action attribute which is automatically filled by ASP.Net. When the URL is output into said attribute it is printed with a full two byte encoding: %u00e5.

当发布回来这似乎是确定当调试代码背后。但是页面实际上做了一个重定向到自己(由于某些其他原因),重定向位置头如下所示:位置:/myFolder/MyPage.aspx?Param1=%C3%A5

When posting back this seems to be ok when debugging the code behind. However the page actually does a redirect to itself (for some other reason) and the redirect location header looks like this: Location: /myFolder/MyPage.aspx?Param1=%C3%A5

因此%e5 已翻译为%C3%A5 <

So the %e5 has been translated to %C3%A5 which breaks the output somehow.

在HTML文本中,破损的字符看起来像&#195;&#165;

In HTML text the broken characters look like &#195;&#165; after having been output via HttpUtility.HtmlEncode.

整个web应用程序都是ISO8859-1编码的。

The entire web application is ISO8859-1 encoded.

PS。当在发布表单之前从action属性中的输出%u00e5 中删除​​u00时,一切都输出得很好。但错误似乎是从%e5 %C3%A5 的翻译。

PS. When removing the u00 from the output %u00e5 in the action attribute before posting the form, everything is output nicely. But the error seems to be the translation from %e5 to %C3%A5. (And of course the self redirect, but that's another matter.)

任何指针?

推荐答案

我最终得到的解决方案是手动编码重定向URL。

The solution I ended up with was encoding the redirect URL manually.

public void ReloadPage()
{
    UrlBuilder url = new UrlBuilder(Context, Request.Path);
    foreach (string queryParam in Request.QueryString.AllKeys)
    {
        string queryParamValue = Request.QueryString[queryParam];
        url.AddQueryItem(queryParam, queryParamValue);
    }
    Response.Redirect( url.ToString(), true);
}

url.AddQueryItem基本上是HttpContext.Server.UrlDecode(queryParamValue) url.ToString构建查询字符串,并为每个查询项HttpContext.Server.UrlEncode(queryParamValue)。

The url.AddQueryItem basically does HttpContext.Server.UrlDecode(queryParamValue) and the url.ToString builds the query string and for each query item does HttpContext.Server.UrlEncode( queryParamValue).

UrlBuilder是一个已经存在于库中的类,所以一旦我发现问题,意识到C#/。Net没有提供工具,编码的修复很快:)

The UrlBuilder is a class already present in our library, so once I found the problem and realized that C#/.Net didn't provide tools for this, coding the fix was quick :)

这篇关于回发后的编码损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆