Global.asax文件說明

Global.asax是咱們的底層文件,第一次的IIS請求都會先去執行它裏面的文件,因此學會它裏面的函數是很是有必要的。並且咱們老是忽略這裏的知識點,總以爲這是沒必要須的,其實咱們錯了,這裏纔是程序的根本。瀏覽器

 

文件代碼:服務器

 

/// <summary>
/// 全部的應用,狀態,程序被訪問,用戶退出,均可以找到。,
/// </summary>
public class Global : System.Web.HttpApplication
{
    /// <summary>
    /// 這裏是IIS請求一開始執行,就執行一遍這個方法。後面還有個Application_End方法。
    /// </summary>
    /// 程序啓動的時候都會執行這個函數(一次),至關於Main函數,
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Application_Start(object sender, EventArgs e)
    {
        File.AppendAllText("c:/1.txt",DateTime.Now.ToString()+"Application_Start");
    }
    /// <summary>
    /// 這裏的Session是服務器端爲每個瀏覽器保存數據開闢的臨時存儲空間,當瀏覽器關閉或者切換用戶就會從新開闢內存來保存Session
    /// 每個瀏覽器的訪問網頁都會有一個有一個Session
    /// 一個瀏覽器的一個用戶公用了一個Session ,當用戶主動退出的時候
    /// </summary>
    /// 統計當前在線人數
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Session_Start(object sender,EventArgs e)
    {
        //這裏至關於創建的臨時會話同樣。
        HttpContext.Current.Session.Abandon();//銷燬Session   ,15分鐘自動取消
    }
    /// <summary>
    /// 請求的時候作一些處理。(每個應用都會觸發這裏)        
    /// </summary>
    /// <param name="sender"></param>
    /// 查看當前請求的URL,經過這個(HttpContext.Current.Request.URL)在
    /// 快速監聽裏面查看總共請求了哪些URL
    /// <param name="e"></param>
    protected void Application_BeginRequest(object sender,EventArgs e)
    {
        //實現功能,屏蔽IP
        if (HttpContext.Current.Request.UserHostAddress== "192.168.1.102")
        {
            HttpContext.Current.Response.Write("這裏就能夠把本身電腦的IP地址屏蔽掉了。");
        }
        else
        {
            HttpContext.Current.Response.Write("");
        }
        //防盜獵
    }
 
    protected  void Application_AuthentiateRequest(object sender,EventArgs e)
    {
 
    }
    /// <summary>
    /// 異常的處理模塊
    /// </summary>
    /// 調用這裏的函數來曝出異常信息
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Application_Error(object sender,EventArgs e)
    {
 
    }
 
    /// <summary>
    /// 斷開會話,15超時的時候調用這個
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Session_End(object sender,EventArgs e)
    {
         
    }
    /// <summary>
    /// 程序被關閉的時候執行一次,IIS被關閉才執行。
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Application_End(object sender,EventArgs e)
    {
 
    }
}

 

 函數的說明:函數

Global.asax這個文件是執行IIS請求必進過的文件,是很是重要。對於Web應用而言是聲明週期的一個事件響應的地方。 Global類,它繼承自System.Web.HttpApplication,它的做用是定義 ASP.NET 應用程序中的全部應用程序對象共有的方法、屬性和事件。 此類是用戶在 Global.asax 文件中所定義的應用程序的基類。*:Application_Start() 此函數是咱們程序剛啓動的時候調用的函數,至關於咱們C語言時候的Main函數同樣,都是程序一開始執行的函數,能夠將一些須要初始化的函數,方法,寫在這裏,好比路由,日誌,IOC,DI,區域,文件等,關閉的時候有個對應的方法Application_End()函數學習

 

protected void Application_Start()
{
    EngineContext.Initialize(false);
 
    var dependencyResolver = new ControllerDependencyResolver();
    DependencyResolver.SetResolver(dependencyResolver);
    GlobalConfiguration.Configuration.DependencyResolver = dependencyResolver;
    AreaRegistration.RegisterAllAreas();
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

 

 

protected void Application_End(object sender,EventArgs e)
{
 
}

 *:Application_Error() 異常處理函數,當應用程序中出現未捕獲的異常,此函數被調用,這裏用HttpContext.Current.Server.GetLastError()來得到異常信息,能夠將其保存到log4Net記錄到日誌中。spa

protected void Application_Error(object sender, EventArgs e)
{
    var exception = Server.GetLastError();
    LogException(exception);
 
}

 *:Session_Start() 服務端Session信息,這裏是每個瀏覽器去訪問服務器,服務器都會爲其建立內存空間,即Session來保存它的一些信息,咱們打開的一些網頁都在 一個Session中進行訪問。(好像是15分鐘自動掉線,至關於一次時間有限的會話。)一個瀏覽器的一個用戶公用一個Session,當用戶主動退出的 時候Session就會被關閉,調用下面的函數來關閉它,Session_End()。能夠在它裏面來統計當前在線人數等。日誌

HttpContext.Current.Session.Abandon();//銷燬Session   ,15分鐘自動取消

 

protected void Session_Start(object sender,EventArgs e)
{
    //這裏至關於創建的臨時會話同樣。
    HttpContext.Current.Session.Abandon();//銷燬Session   ,15分鐘自動取消
}

 

protected void Session_End(object sender,EventArgs e)
{
     
}

 *:Application_BeginRequest() 請求的時候都會訪問這個函數,每個應用也會觸發這裏,咱們能夠經過下面的函數來查看當前的請求URL, (HttpContext.Current.Request.URL)。能夠在快速監聽裏面進行查看。看一個網頁總共請求了幾回URL。在這裏能夠屏蔽 IP,防盜獵圖片等功能。對象

protected void Application_BeginRequest(object sender,EventArgs e)
{
    //實現功能,屏蔽IP
    if (HttpContext.Current.Request.UserHostAddress== "192.168.1.102")
    {
        HttpContext.Current.Response.Write("這裏就能夠把本身電腦的IP地址屏蔽掉了。");               
    }
    else
    {
        HttpContext.Current.Response.Write("");
    }
    //防盜獵
}

 *:這個函數我也不是很清楚,等之後學習的時候在補吧。blog

protected  void Application_AuthentiateRequest(object sender,EventArgs e)
{
 
}
相關文章
相關標籤/搜索