ASP.NET全域應用程式類

來源:互聯網
上載者:User

標籤:blog   http   os   io   ar   strong   檔案   art   div   

ASP.NET全域應用程式類引自老Man的部落格http://shijiaqi1066.iteye.com/blog/1728603這個儲存到自己的部落格裡邊方便以後學習查看

通用檔案

 

建立通用檔案

Visual Studio 【添加新項】-->【Web】-->【全域應用程式類】

產生Global類。

 

一個webapplication只能有一個全域應用程式類。如果建立的是否發現沒有【全域應用程式類】,則表示已經該類已經存在,如果需要建立,則先刪除舊的,再建立。

 

Global類:

 

 

C#代碼  
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Security;  
  6. using System.Web.SessionState;  
  7.   
  8. namespace WebApplication1  
  9. {  
  10.     public class Global : System.Web.HttpApplication  
  11.     {  
  12.   
  13.         protected void Application_Start(object sender, EventArgs e)  
  14.         {  
  15.             // 在應用程式啟動時啟動並執行代碼。  
  16.             // 程式第一次獲得請求時,該方法被執行。  
  17.   
  18.         }  
  19.   
  20.         protected void Session_Start(object sender, EventArgs e)  
  21.         {  
  22.             // 在新會話啟動時啟動並執行代碼。  
  23.             // 該方法被調用,表示當前有一個新的會話產生了。  
  24.         }  
  25.   
  26.         protected void Application_BeginRequest(object sender, EventArgs e)  
  27.         {  
  28.             // 在新請求啟動時允許的代碼。  
  29.             // 每一次請求都會觸發該方法。  
  30.         }  
  31.   
  32.         protected void Application_AuthenticateRequest(object sender, EventArgs e)  
  33.         {  
  34.             // 在應用程式啟動時啟動並執行代碼。  
  35.             // 程式第一次獲得請求時,該方法被執行。  
  36.         }  
  37.   
  38.         protected void Application_Error(object sender, EventArgs e)  
  39.         {  
  40.             // 在出現未處理的錯誤時啟動並執行代碼  
  41.             // 擷取異常資訊並處理:HttpContext.Current.Server.GetLastError();  
  42.         }  
  43.   
  44.         protected void Session_End(object sender, EventArgs e)  
  45.         {  
  46.             // 在會話結束時啟動並執行代碼。   
  47.             // 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為InProc 時,才會引發 Session_End 事件。  
  48.             // 如果會話模式設定為 StateServer  或 SQLServer,則不會引發該事件。  
  49.         }  
  50.   
  51.         protected void Application_End(object sender, EventArgs e)  
  52.         {  
  53.             //  在應用程式關閉時啟動並執行代碼  
  54.         }  
  55.     }  
  56. }  

 

應用:

Session_Start()方法:統計線上人數。

Application_BeginRequest()方法:屏蔽IP,防止盜鏈。對所以圖片加浮水印。

Application_AuthenticateRequest()方法:驗證方法。

Application_Error()方法:

 

補充:

1.取得當前請求url:HttpContext.Current.Request.Url

2.手動登出Session:HttpContext.Current.Session.Abandon()

3.向檔案添加文本資訊,若檔案不存在,則先建立:File.AppendAllText("c:1.txt",DateTime.Now.ToString())

4.取得訪問網站的請求的ip:HttpContext.Current.Request.UserHostAddress;

5.向頁面列印輸出:HttpContext.Current.Request.Write("已被屏蔽!");

6.列印輸出結束要調用:HttpContext.Current.Request.End();

7.取得請求類型:HttpContext.Current.Request.ContentType

8.擷取用戶端上次請求的url資訊:HttpContext.Current.Request.UrlReferrer;   (說明:該屬性是Uri類的執行個體。)

9.擷取uri對象的網域名稱:uri.Host;

(擷取用戶端上次請求的url的網域名稱:HttpContext.Current.Request.UrlReferrer.Host;)

10.擷取異常資訊:HttpContext.Current.Server.GetLastError();

ASP.NET全域應用程式類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.