ASP.NET學習筆記(四)-模板頁,HttpHandler,IHttpModule應用

來源:互聯網
上載者:User

6.模版頁   (1).加入模版頁可以是將圖片粘貼到網站,最好建立一個檔案夾,這樣看起來分類較細   (2).在網站建立項裡建立一個建立一個模版頁,即.master,加入樣式(主標題在div外面寫字,可用左右鍵切換,添加一個Image控制項,將ImageUrl選為內連的圖片,最好樣式的長寬住設計檢視裡搞好,不然就得用相應的 td的 width ="15%" bordergrouder-color="Grey" 或:style=" width:100px; bordergrouder-color:50%" 來改,)   (3).修改本來繼承其他頁面範本的代碼:        protected void Page_PreInit(object sender, EventArgs e)    {        this.MasterPageFile = "~/MasterPage2.master";    }7.HttpHandler的使用    (1). 在ProcessRequest 事件中寫反饋頁面    (2).在Config的System.web下的Httphandler 下add自己的 verb, path寫被訪問的尾碼名 (隨便寫), type 是(寫為絕對路徑)    具體代碼如下:  //這裡的type沒有引入命名空間,所以就不存在 完整路徑,直接是類名 using System; using System.Collections.Generic; using System.Linq; using System.Web;/// ///MyHttpHandler 的摘要說明/// public class MyHttpHandler:IHttpHandler  //記住是IHttpHandler ,不是IHttpAnyHandler。。。。。。{    public MyHttpHandler()    {        //        //TODO: 在此處添加建構函式邏輯        //    }    public bool IsReusable    {        get { return false; }    }    public void ProcessRequest(HttpContext context) //這是一個返回請求的頁面,依然用HttpResponse 的執行個體去Write網頁..........    {        //對於錯誤報表等返回很有效,也可以作為Session的驗證。。。不反饋前台.............        //也可以返回與前台頁面相同的頁面,不過要write包的麻煩些        記住是在這個事件裡寫        HttpResponse response = context.Response;        response.Write("");        response.Write("");        response.Write(" 這是來MyHandler的相應");        response.Write("");        response.Write("");        response.Write(" 這是來MyHandler的相應");        response.Write("");    }}        在Web.config中:      8.Http模組的IHttpModule類的使用(類別模組,類似與模版類模版)  (1).一般的方法是繼承的IHttpModule,裡面有Begin 的require 即在用戶端的Http請求具體頁面發過來的之前,就先請求該模組的事件,類似的類方法還有End require,即在最後的請求中才請求該模組的資訊(一般放的是頁尾的著作權資訊,每頁必有)  (2).該類的事件都是在HTML的頁面之外的情況...   (3).在Inti裡寫初始化,連續兩次按tab鍵,出方法using System;using System.Collections.Generic;using System.Linq;using System.Web;/// ///MyHttpMudule 的摘要說明/// public class MyHttpMudule:IHttpModule{    public MyHttpMudule()    {        //        //TODO: 在此處添加建構函式邏輯        //    }    public void Dispose()    {        return;    }    public void Init(HttpApplication context)    {        context.BeginRequest += new EventHandler(context_BeginRequest); //連續按兩次Tab鍵出下面的方法        context.EndRequest += new EventHandler(context_EndRequest);        context.AcquireRequestState += new EventHandler(context_AcquireRequestState);    }    /*        void context_AcquireRequestState(object sender, EventArgs e)        {            HttpApplication application = sender as HttpApplication;            System.Web.SessionState.HttpSessionState session = application.Session; //擷取Session            if (null == session["username"])            {                             HttpResponse response = application.Response;                 response.Redirect("~/UserLogin.aspx");                        response.Redirect("~/UserLogin.aspx/"); //導向到新的頁面去,用response.Redirect 重新導向                 但是這裡會造成死迴圈,因為每個頁面都會重複進行Session的驗證,由於Session裡為空白,所以會跳回自己                               response.Redirect("~/UserLogin.aspx");                               application.Server.Transfer("~/UserLogin.aspx");//解決的辦法代替 response.Redirect("~/UserLogin.aspx");              //這裡直接請求UserLogin.aspx,而不經過Session模組                      }*/  //或者在之前判斷一下是否是UserLogin.aspx頁面,如果是,就不需判斷,直接返回    void context_AcquireRequestState(object sender, EventArgs e)    {        HttpApplication application = sender as HttpApplication;        string filepath = application.Request.FilePath;//返回請求的檔案路徑        string FileName = VirtualPathUtility.GetFileName(filepath);//返迴文件的最後的完整檔案名稱        if (FileName == "UserLogin.aspx") return;  //導向到新的頁面去,用response.Redirect 重新導向,跳轉到新的頁面        System.Web.SessionState.HttpSessionState session = application.Session; //擷取Session        if (null == session["username"])        {            HttpResponse response = application.Response;            response.Redirect("~/UserLogin.aspx");             /*               response.Redirect("~/UserLogin.aspx/"); //導向到新的頁面去,用response.Redirect 重新導向             但是這裡會造成死迴圈,因為每個頁面都會重複進行Session的驗證,由於Session裡為空白,所以會跳回自己                            response.Redirect("~/UserLogin.aspx");                           application.Server.Transfer("~/UserLogin.aspx");//解決的辦法代替 response.Redirect("~/UserLogin.aspx");              * 在尾碼名不變的情況下直接顯示轉移網頁的內容,該方法不會再次類比提交的用戶端,所以不會再次經過Http模組,該模組可以隨時的進行加刪,所以很方便,這是單點登入的很好技巧             */            //或者在之前判斷一下是否是UserLogin.aspx頁面,如果是,就不需判斷,直接返回        }    }    void context_EndRequest(object sender, EventArgs e)    {        HttpApplication application = sender as HttpApplication;        HttpResponse response = application.Response;        response.Write("這是一個來自HTTP模組的著作權資訊");//通過源碼可以看出,是在html的題尾    }    void context_BeginRequest(object sender, EventArgs e)//這是在未請求default 頁面之前進行的,即所有頁面都會有的東西,但是要在config中的httphandler裡註冊,所以可以使用這個作為session。。。。。。全域網站驗證    {        HttpApplication applicition = sender as HttpApplication;        HttpResponse response = applicition.Response;        response.Write("這是一個來自HTTP模組的題頭");//通過源碼可以看出,是在所有html最前面    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.