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

标签 asp.net global-asax

我为asp.net应用程序创建了一个global.asax文件。我在session_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
}

}

最佳答案

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

关于asp.net - asp.net-asp.net 2.0应用程序中的global.asax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6327387/

相关文章:

asp.net - 为什么在调用 Application_Error() 时 Response.StatusCode 设置为 200?

asp.net - global.asax 范围和生命周期说明

iis - 使用 global.asax 有哪些替代方法?

c# - 在 Global.asax.cs 中获取客户端 ip 地址 - 这可能吗?

asp.net - 选中项目时,UpdatePanel 内的复选框列表会触发完整回发

asp.net-mvc-3 - 执行子请求时出错

javascript - 未在服务器控件中评估服务器标记

c# - 在经典 ASP 上构建 .net 应用程序

css - 两个没有换行符的内联表

javascript - 将 jquery 与 ASP.net 和 C# 结合使用