注意:
不是 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 }
带有 Controller find
、操作 square
和 id äa
(在本例中)
这让我感到非常惊讶,因为 .NET 在涉及变音符号和 UTF-8 时通常非常谨慎和稳健。我在这里特别迷路,因为我无法在本地重现该问题。我假设这是一个服务器设置,但 Azure 在这一点上非常稀疏。以前有没有人经历过类似的行为?
最佳答案
这是我的代码的一部分,我没有发布,重写规则。我只是在仔细查看 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 中检查我的 URL .cs
像这样:
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;
}
}
我对这种方法并不完全满意;它感觉很臭,但在我找到更好的解决方案之前,这已经足够了。
关于c# - RedirectToAction() 打破元音变音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34053931/