![](/img/trans.png)
[英]ASP.NET Global.asax Application_PreRequestHandlerExecute
[英]Why Does Hit Counter(Application Variable) from Global.asax set to '0' after some time in ASP.NET?
每個搜索數據庫都有大約50個應用程序變量。 從單個Search.aspx頁中查詢的總數為50個searchDB,具體取決於它連接到特定數據庫的URL中傳遞的查詢字符串。
例如:如果Search.aspx?li = 1,則連接到1SearchDB,如果Search.aspx?li = 2,則連接到2SearchDB,..... 50SearcgDB。
我將根據URL中的QueryString維護每個searchDB的總訪問者,並增加GLOBAL.ASAX文件中的應用程序變量。
在Global.asax中:
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application["1"] = 0;
Application["2"] = 0;
.
.
Application["50"] = 0;
}
在Search.aspx.cs的Page_Load中:
int LocalBody = Convert.ToInt32(Request.QueryString["li"]);
public void Page_Load(object sender, EventArgs e)
{
Label1.Text = GetHits(LocalBody).ToString();
}
private int GetHits(int LocalBody)
{
int counter=0;
switch (LocalBody)
{
case 1:
Application["1"] = (int)Application["1"] + 1;
counter=(int)Application["1"];
break;
case 2:
Application["2"] = (int)Application["2"] + 1;
counter=(int)Application["2"];
break;
.
.
case 50:
Application["50"] = (int)Application["50"] + 1;
counter=(int)Application["50"];
break; default:
break;
}
return counter;
}
現在的問題是,當我啟動應用程序時,經過一段時間(30至40分鍾),它會重新啟動計數器。 當我使用它時,它工作正常! 為什么會這樣?
這是因為在Application_Start
程序重新啟動時會觸發Application_Start
並重置您的計數器,並且可能通過您的池設置自動重新啟動應用程序。
希望它對HydTechie有幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.