asp.net 2.0应用程序中的asp.net-global.asax

asp.net 2.0应用程序中的asp.net-global.asax,asp.net,global-asax,Asp.net,Global Asax,我为asp.net应用程序创建了一个global.asax文件。我在session\u start方法中运行了一些代码。代码不会被执行。在asp.net 2.0中使用global.asax文件是否有某种类型的过程 我有asax文件本身和一个代码隐藏文件 谢谢大家! 编辑: asax文件: <%@ Application Codebehind="Global.asax.cs" Inherits="GrowUp.Global" %> 代码隐藏文件: using System; us

我为asp.net应用程序创建了一个global.asax文件。我在session\u start方法中运行了一些代码。代码不会被执行。在asp.net 2.0中使用global.asax文件是否有某种类型的过程

我有asax文件本身和一个代码隐藏文件

谢谢大家!

编辑: asax文件:

<%@ Application Codebehind="Global.asax.cs" Inherits="GrowUp.Global" %>

代码隐藏文件:

using System;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
namespace Ligdol 
{
/// <summary>
/// Summary description for Global.
/// </summary>
public class Global : System.Web.HttpApplication
{
    /// <summary>
    /// Required designer variable.
    /// </summary>
    private System.ComponentModel.IContainer components = null;

    public Global()
    {
        InitializeComponent();
    }   

    protected void Application_Start(Object sender, EventArgs e)
    {
        Application["HostName"] = System.Configuration.ConfigurationSettings.AppSettings["HostName"];
        Application["counter"] = 1;
    }

    protected void Session_Start(Object sender, EventArgs e)
    {
        // Get the count from the application variable
    int counter = int.Parse(Application["counter"].ToString());
    //Check if a cookie exists.
    if(HttpContext.Current.Request.Cookies["ligdolVersion"] != null)
    {
        //If a cookie exists, we need to redirect the user to the respective site.
        if(HttpContext.Current.Request.Cookies["ligdolVersion"].ToString() == "new")
        {
            Response.StatusCode = 302;
            Response.Status = "Moved temporarily";
            Response.Redirect("http://beta.ligdol.co.il");
            return;
        }
        else if(HttpContext.Current.Request.Cookies["ligdolVersion"].ToString() == "old")
        {
            return;
        }
    }
    else if (counter == 40)
    {
        // If a cookie does not already exist,
        //we need to check if the user is to be allowed to continue to the old site
        //or be redirected to the new site.


        //Note in a file that a user was redirected, so we can get an estimate of how many are being redirected.
        System.IO.TextWriter tw = new System.IO.StreamWriter(@"redirect.log");
        tw.WriteLine("Redirected to new site.");
        tw.Close();
        // Reset counter.
        Application["counter"] = 1;
        //write cookie made to expire in 30 days, by then the experiment will be over (we hope!).
        HttpCookie cookie = new HttpCookie("ligdolVersion");
        DateTime dtNow = DateTime.Now;
        TimeSpan tsSpan = new TimeSpan(30, 0, 0, 0, 0);
        cookie.Expires = dtNow + tsSpan;
        cookie.Value = "new";
        Response.Cookies.Add(cookie);
        Response.Redirect("http://beta.ligdol.co.il");
        return;
    }
    else
    {
        System.IO.TextWriter tw = new System.IO.StreamWriter(@"redirect.log");
        tw.WriteLine("Redirected to old site.");
        tw.Close();
        HttpCookie cookie = new HttpCookie("ligdolVersion");
        DateTime dtNow = DateTime.Now;
        TimeSpan tsSpan = new TimeSpan(30, 0, 0, 0, 0);
        cookie.Expires = dtNow + tsSpan;
        cookie.Value = "old";
        Response.Cookies.Add(cookie);
        return;
    }

    }

    protected void Application_BeginRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_EndRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_AuthenticateRequest(Object sender, EventArgs e)
    {

    }

    protected void Application_Error(Object sender, EventArgs e)
    {

    }

    protected void Session_End(Object sender, EventArgs e)
    {

    }

    protected void Application_End(Object sender, EventArgs e)
    {

    }

    #region Web Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
        this.components = new System.ComponentModel.Container();
    }
    #endregion
}
使用系统;
使用系统集合;
使用系统组件模型;
使用System.Web;
使用System.Web.SessionState;
名称空间Ligdol
{
/// 
///全局搜索的摘要说明。
/// 
公共类全局:System.Web.HttpApplication
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
公共全球()
{
初始化组件();
}   
受保护的无效应用程序\u启动(对象发送方,事件参数e)
{
应用程序[“主机名”]=System.Configuration.ConfigurationSettings.AppSettings[“主机名”];
应用程序[“计数器”]=1;
}
受保护的无效会话\u启动(对象发送方,事件参数e)
{
//从应用程序变量获取计数
int counter=int.Parse(应用程序[“counter”].ToString());
//检查cookie是否存在。
if(HttpContext.Current.Request.Cookies[“ligdolVersion”]!=null)
{
//如果存在cookie,我们需要将用户重定向到相应的站点。
if(HttpContext.Current.Request.Cookies[“ligdolVersion”].ToString()=“new”)
{
Response.StatusCode=302;
Response.Status=“暂时移动”;
响应。重定向(“http://beta.ligdol.co.il");
返回;
}
else if(HttpContext.Current.Request.Cookies[“ligdolVersion”].ToString()=“old”)
{
返回;
}
}
否则如果(计数器==40)
{
//如果cookie不存在,
//我们需要检查是否允许用户继续访问旧站点
//或者被重定向到新站点。
//请注意,在一个文件中,一个用户被重定向,因此我们可以估计有多少用户被重定向。
System.IO.TextWriter tw=新的System.IO.StreamWriter(@“redirect.log”);
tw.WriteLine(“重定向到新站点”);
tw.Close();
//重置计数器。
应用程序[“计数器”]=1;
//写下30天后过期的cookie,到那时实验就结束了(我们希望!)。
HttpCookie cookie=新的HttpCookie(“ligdolVersion”);
DateTime dtNow=DateTime.Now;
TimeSpan tsSpan=新的TimeSpan(30,0,0,0,0);
cookie.Expires=dtNow+tsSpan;
cookie.Value=“新建”;
Response.Cookies.Add(cookie);
响应。重定向(“http://beta.ligdol.co.il");
返回;
}
其他的
{
System.IO.TextWriter tw=新的System.IO.StreamWriter(@“redirect.log”);
tw.WriteLine(“重定向到旧站点”);
tw.Close();
HttpCookie cookie=新的HttpCookie(“ligdolVersion”);
DateTime dtNow=DateTime.Now;
TimeSpan tsSpan=新的TimeSpan(30,0,0,0,0);
cookie.Expires=dtNow+tsSpan;
cookie.Value=“旧”;
Response.Cookies.Add(cookie);
返回;
}
}
受保护的无效应用程序\u BeginRequest(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u EndRequest(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u AuthenticateRequest(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u错误(对象发送方,事件参数e)
{
}
受保护的无效会话\u结束(对象发送方,事件参数e)
{
}
受保护的无效应用程序\u结束(对象发送方,事件参数e)
{
}
#区域Web表单设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{    
this.components=new System.ComponentModel.Container();
}
#端区
}

}问题在于代码隐藏文件。一旦我将代码内联到asax文件中,它就工作了。这可能是旧网站项目的唯一解决方案。

您可以发布您尝试过的代码吗?当然可以。很抱歉我想asp.net 2.0中的global.asax文件可能存在一些已知问题。在原始帖子中发布的代码。您是否在Global.asax中添加断点并进行检查?在本地它可以工作。我点击断点,它通过并保存cookie和所有内容。在远程服务器上,什么都没有发生。@Elad Lachmi:你是说“在远程服务器上,什么都没有发生”。你不能到达断点,但代码还是执行了??