gpt4 book ai didi

c# - Global.asax Application_BeginRequest 中的 ToLower() 尖峰 CPU 和轰炸应用程序

转载 作者:行者123 更新时间:2023-11-30 15:05:55 24 4
gpt4 key购买 nike

希望有人可以阐明我们遇到的这个问题,因为我在这里不知所措。

首先,一点背景:

我在几周前为我们的应用程序重写了 URL 重写并实现了它。我使用 global.asax 文件中的 Application_BeginRequest() 执行此操作,除了我的一个小疏忽,我们的应用程序一切正常。当我重写 URL 时,我只是检查用户请求的路径中是否存在某些关键字,然后相应地重写路径。非常直接的东西,而不是在这里发明轮子。干代码,真的。但是,我要检查的文本都是小写的,而路径可能有不同的大小写。

例如:

string sPath = Request.Url.ToString();
sPath = sPath.Replace(Request.Url.Scheme + "://", "")
.Replace(Request.Url.Host, "");
if (sPath.TrimStart('/').TrimEnd('/').Split('/')[0].Contains("reports") && sPath.TrimStart('/').TrimEnd('/').Split('/').Length > 2) {
string[] aVariables = sPath.TrimStart('/').TrimEnd('/').Split('/');
Context.RewritePath("/app/reports/report-logon.aspx?iLanguageID=" + aVariables[1] + "&sEventCode=" + aVariables[2]);
}

...如果有人以/Reports/的身份进入页面,规则将不匹配,他们将因此收到 404 错误。

不过,我认为修复起来很简单。只需要将请求的路径字符串强制为小写,这样我尝试与之匹配的任何内容都会查看请求路径的小写版本,并在上述情况下成功匹配。所以我将代码调整为:

    string sPath = Request.Url.ToString();
sPath = sPath.Replace(Request.Url.Scheme + "://", "")
.Replace(Request.Url.Host, "");
sPath = sPath.ToLower(); // <--- New line
if (sPath.TrimStart('/').TrimEnd('/').Split('/')[0].Contains("reports") && sPath.TrimStart('/').TrimEnd('/').Split('/').Length > 2) {
string[] aVariables = sPath.TrimStart('/').TrimEnd('/').Split('/');
Context.RewritePath("/app/reports/report-logon.aspx?iLanguageID=" + aVariables[1] + "&sEventCode=" + aVariables[2]);
}

通过此修复,当我请求与 URL 重写相匹配的任何 URL 时,服务器上的 CPU 会达到 100%,并且我的整个应用程序会崩溃。我取出 .ToLower(),终止应用程序池,应用程序又完全正常了。

我是不是漏掉了什么!?!?是什么赋予了?为什么这么简单的方法会导致我的应用程序爆炸? .ToLower() 在我们应用程序的其他任何地方都有效,虽然我没有广泛使用它,但我在应用程序的其他地方使用它非常成功。

最佳答案

不确定为什么 ToLower 会导致这种情况(我唯一能想到的是它正在修改 request.url,这会使 asp.net 陷入疯狂),但是有一个简单的解决方法:使用 ignorecase 比较而不是将一切都转换成更低的。

改变:

sPath.TrimStart('/').TrimEnd('/').Split('/')[0].Contains("reports")

到:

sPath.TrimStart('/').TrimEnd('/').Split('/')[0].IndexOf("reports",  StringComparison.InvariantCultureIgnoreCase) != -1

并删除您的 ToLower 逻辑。

关于c# - Global.asax Application_BeginRequest 中的 ToLower() 尖峰 CPU 和轰炸应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8568192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com