RedirectToAction() 打破元音变音

RedirectToAction() breaks umlauts

注: 不是 RedirectToAction()导致的问题,而是在web.config

中重写规则

原问题: 使用 RedirectToAction() 时出现奇怪的行为,我无法在本地重现(调试)。在某些时候,我的路由值从 äa 更改为 äa,但仅限于服务器 (Azure)。我添加了日志记录以查明确切位置并在此处结束:

RedirectToAction("square", new { id = criteria.Trim().ToLower() });

本地正确重定向到 /find/square/äa,但在服务器上它最终到达 /find/square/äa。我记录了每一步,当我的字符串传递给 RedirectToAction 时它仍然完好无损(即使在 Trim()ToLower() 之后),但在重定向后损坏了。我使用的是默认路由

routes.MapRoute("Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }

with controller find, action square and id äa (在这种情况下)

这让我感到非常惊讶,因为 .NET 在涉及变音符号和 UTF-8 时通常非常谨慎和稳健。我在这里特别迷路,因为我无法在本地重现该问题。我假设这是一个服务器设置,但 Azure 在这一点上非常稀疏。以前有人经历过类似的行为吗?

这是我的代码的一部分,我没有 post 重写规则。我只是在仔细查看 Fiddler 中的重定向后才发现。事实上,我在我故意触发的重定向之后发现了一个额外的重定向,它是由我的重写规则之一引起的:

    <rule name="Lowercase Path" stopProcessing="true" >
      <match url="[A-Z]" ignoreCase="false" />
      <conditions>
        <add input="{REQUEST_METHOD}" matchType="Pattern" pattern="POST" ignoreCase="true" negate="true" />
        <add input="{URL}" pattern="^.*\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$" negate="true" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="{tolower:{URL}}" redirectType="Permanent" />
    </rule>

实际上是下面一行:

<action type="Redirect" url="{tolower:{URL}}" redirectType="Permanent" />

只要我禁用这条规则,一切都会正常。我的其他不使用 {ToLower:} 的规则没有产生任何问题。我不确定,为什么这不会破坏本地的东西。在做我的研究时,我发现了一篇文章,描述了微软如何修补一个损坏的重写模块,该模块导致类似的元音变音问题。这是推测,但也许我的 Azure 实例上的重写版本未打补丁 - 或者它完全不同。

不幸的是,除了使用 {tolower:{URL}} 的重写规则之外,几乎没有其他选择,所以我不得不做一个相当丑陋的解决方法,并在 Global.asax.cs 中检查我的 URL ,如下所示:

    protected void Application_BeginRequest(object sender, EventArgs e)
    {
        var pathAndQuery = System.Web.HttpUtility.UrlDecode(Request.Url.PathAndQuery);

        if (pathAndQuery == null || Regex.IsMatch(pathAndQuery, @"^.*\.(axd|css|js|jpg|jpeg|png|gif|txt|xml|svg|pdf)$"))
        {
            return;
        }

        if (Regex.IsMatch(pathAndQuery, ".*[A-Z].*"))
        {
            Response.Redirect(pathAndQuery.ToLower(), false);
            Response.StatusCode = 301;
        }
    }

我对这种方法不是很满意;感觉很臭,但在我找到更好的解决方案之前,这已经足够了。