代码改变世界

DotText缺少Global.asax会出现什么情况?!

  无常  阅读(1360)  评论(1编辑  收藏  举报
曾经在这里发过这篇POST《求助:GetVaryByCustomString()不工作
但没有人回答,google和baidu都没有答案
昨天看了dudu的《搜索引擎, 请手下留情》之后
我也想给统计一下二频博客的访问者
在Global.asax.cs中
    public class Global : System.Web.HttpApplication
    {
        
public static System.Collections.Hashtable  htBrowser = new Hashtable();

        
protected void Application_BeginRequest(Object sender, EventArgs e)
        {
            
string userAgent = Context.Request.UserAgent;
            
//Context.Request.UserAgent
            
//Application.Lock();  反正要求精度不高,为了尽量不影响性能,不用锁了
            if( htBrowser.Contains(userAgent) )
            {
                
int c = Convert.ToInt32(htBrowser[userAgent].ToString());
                htBrowser[userAgent] 
= c+1;
            }
else
            {
                htBrowser.Add(userAgent,
1);
            }
            
//Application.UnLock();
        }

}
新加一个test.aspx
        private void Page_Load(object sender, System.EventArgs e)
        { 
  
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(
"<table border=\"1\" cellpadding=\"5\"><tr>");
            sb.Append(
"<td>Browser</td><td>Count</td>");
            System.Collections.IDictionaryEnumerator ide 
= Dottext.Global.htBrowser.GetEnumerator(); 
            
while (ide.MoveNext()) 
            { 
                sb.Append(
string.Format( "<tr><td>{0}</td><td>{1}</td></tr>", ide.Key, ide.Value) ); 
            } 
            sb.Append(
"</tr></table>");
            Response.Write( sb.ToString();
}


在本机测试成功,但上传到服务器后就不工作了
test.aspx总是没内容
百思不得其解
这让我想起以缓存不更新的事
突然想到会不会是Application_BeginRequest根本不执行呢
那又是为什么会不执行这段代码
灵感一现
看了一下服务器上的文件,果然是没有Global.asax
把这个文件上传,OK!
包括以前自定义缓存的问题也解决了

回顾一下问题的原因
因为我用了一个build.bat批处理,把CNBlogsDottext目录中的需要部署的文件都拷贝出来

@echo off
echo - Copying Web Files 
xcopy *.gif ..\builds\release\web\ /s /y /q
xcopy *.jpg ..\builds\release\web\ /s /y /q
xcopy *.txt ..\builds\release\web\ /s /y /q
xcopy *.aspx ..\builds\release\web\ /s /y /q
xcopy *.ascx ..\builds\release\web\ /s /y /q
xcopy *.htm* ..\builds\release\web\ /s /y /q
xcopy *.xml ..\builds\release\web\ /s /y /q
xcopy *.css ..\builds\release\web\ /s /y /q
xcopy *.js ..\builds\release\web\ /s /y /q
xcopy bin\*.dll ..\builds\release\web\bin\ /s /y /q
xcopy *.config ..\builds\release\web\ /s /y /q
xcopy *.xsl ..\builds\release\web\ /s /y /q
但这个批处理里没有把*.asax拷出来
我在部署时也没去检查
呵呵
问题终于解决了
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示