ASP.NET MVC 不理解混合 url 编码 (UTF-8/Latin-1) [英] ASP.NET MVC does not understand mixed url encoding (UTF-8/Latin-1)

查看:25
本文介绍了ASP.NET MVC 不理解混合 url 编码 (UTF-8/Latin-1)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个带参数的网址

http://localhost:8041/Reforge.aspx?name=CyanГ
http://localhost:8041/Reforge.aspx?name=Cyanì

在第一个 URL 中,Firefox 将最后一个字符 (Г) 编码为 %D0%93(在 UTF-8 中正确).在第二个 URL 中,Firefox 将最后一个字符 (ì) 编码为 %EC(在 ISO-8859-1 中正确)

In first URL Firefox encodes last charecter (Г) as %D0%93 (correctly in UTF-8). In second URL Firefox encodes last character (ì) as %EC (correctly in ISO-8859-1)

ASP.NET MVC 可以使用 web.config 中的元素进行配置,以采用 UTF-8 或 ISO-8859-1.但是 Firefox 会根据上下文在编码之间切换.

ASP.NET MVC can be configured using element in web.config to either assume UTF-8 or ISO-8859-1. But Firefox flips between encodings depending on the context.

请注意,UTF-8 可以明确地区分于 Latin-1 编码.

Note that UTF-8 can be unambiguously distinguished from Latin-1 encoding.

有没有办法教 ASP.NET MVC 使用其中一种格式解码参数值?

Is there a way to teach ASP.NET MVC to decode parameter values using either one of the formats?

是否有一个类可以用来解码可以正确处理编码的原始查询字符串?注意 - Firefox 使用 UTF-8 Latin-1 编码 - 但不能同时使用两者.所以我的计划是尝试使用 UTF-8 手动解码,然后查找无效"字符 (FFFD),如果找到 - 尝试使用 Latin-1 解码.

Is there a class that I could use to decode raw query string that would handle encoding correctly? Note - Firefox uses either UTF-8 or Latin-1 encoding - but not both at the same time. So my plan is to try decode manually using UTF-8 and then look for "invalid" character (FFFD), if one is found - try Latin-1 decode.

示例:

Firefox 编码如下:

Firefox encodes as following:

-                                          v   v
http://localhost:8041/Reforge.aspx?name=ArcânisГ 
Firefox turns into  
http://localhost:8041/Reforge.aspx?name=Arc%C3%A2nis%D0%93`  

请注意,UTF8 编码用于两个非 ASCII 字符.

Notice that UTF8 encoding is used for both non-ASCII characters.

-                                          v
http://localhost:8041/Reforge.aspx?name=Arcâ
Firefox turns into
http://localhost:8041/Reforge.aspx?name=Arc%E2

请注意,ISO-8859-1 (Latin-1) 编码用于非 ASCII 字符.

Notice that ISO-8859-1 (Latin-1) encoding is used for the non-ASCII character.

推荐答案

这是我的工作解决方案,有什么改进的方法吗?具体来说,我宁愿扩展框架而不是在动作本身内部处理它.

Here is my working solution, any way to improve on it? Specifically I would rather extend framework instead of handling it inside an action itself.

    private string DecodeNameParameterFromQuery(string query) {
        string nameUtf8 = HttpUtility.ParseQueryString(query, Encoding.UTF8)["name"];
        const char invalidUtf8Character = (char) 0xFFFD;
        if (nameUtf8.Contains(invalidUtf8Character)) {
            const int latin1 = 0x6FAF;
            var nameLatin1 = HttpUtility.ParseQueryString(query, Encoding.GetEncoding(latin1))["name"];
            return nameLatin1;
        }
        return nameUtf8;
    }

这篇关于ASP.NET MVC 不理解混合 url 编码 (UTF-8/Latin-1)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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