.net學習之主版頁面執行順序、jsonp跨域請求原理、IsPostBack原理、伺服器端控制項按鈕Button點擊時的過程、緩衝、IHttpModule 過濾器

來源:互聯網
上載者:User

標籤:style   class   blog   code   http   tar   

1.WebForm使用主版頁面後執行的順序是先執行子頁面中的Page_Load,再執行主版頁面中的Page_Load,請求是先產生主版頁面的控制項樹,然後將子頁面產生的控制項樹填充到主版頁面中,最後輸出

2.WebForm使用主版頁面進行登入時候的驗證

//建立一個頁面父類public abstract class BasePage:Page    {        protected void Page_Load(object sender, EventArgs e)        {            //1.身分識別驗證的方法 Session            if (Session["uinfo"] == null)            {                if (Request.Cookies["uinfo"] == null)                {                    PageHelper.WriteJsMsg("您尚未登入~~~", "/Manage/View/Login.html");                    Response.End();                }                else                {                    //如果Cookie中有使用者id,則 根據id讀取使用者物件 存入 Session                    string strId = Request.Cookies["uinfo"].Value;                    //驗證 strId 是否為 整型                    //驗證成功則讀取資料庫 , 並將 使用者物件 儲存到Session                    Session["uinfo"] = new BLL.Users().GetModel(int.Parse(strId));                    //2.調用子類的 重寫方法                    ChildPageLoad();                }            }                  }        public abstract void ChildPageLoad();    }//頁面需要許可權驗證的都繼承BasePagepublic partial class WebForm3 :BasePage    {              public override void ChildPageLoad()        {            throw new NotImplementedException();        }    }

3.jsonp跨域請求原理,就是使用script標籤可以跨域,請求回來的字串瀏覽器作為js代碼進行執行
jQuery請求的代碼:

$.ajax("url",    type:"get",    dataType:"jsonp",    jsonp:"callback",//發送伺服器的回呼函數名參數    jsonpCallback:"callbackFun",//指定回調的函數名    success:function(){        alert("請求成功");    });function callbackFun(data){    }

4.IsPostBack原理:
是否為表單提交,伺服器端asp.net架構依靠檢測請求報文中的_VIEWSTATE來設定值

5.伺服器端控制項按鈕Button點擊時的過程
第8個事件建立前台頁面對象,11、12之間調用頁面的ProccessRequest方法,打造頁面控制項樹,調用PageLoad,處理非按
鈕點擊事件,處理按鈕點擊事件,Render產生html代碼

瀏覽器端有按鈕被點擊的時候,會產生請求報文到伺服器,被點擊按鈕的name會被傳到伺服器,伺服器端asp.net架構會
根據這個name到控制項樹中找到對應的伺服器控制項,並執行它的事件,從而呼叫事件方法,但是,如果請求報文中包含多個
按鈕名字,則伺服器端只執行最後一個按鈕的事件方法。

6.緩衝
(1)ASP.NET 網頁輸出快取
<%@ OutputCache Duration="30" VaryByParam="id;name"%>
<%--VaryByParam 必需屬性,可以為 VaryByParam="none" 或 VaryByParam="*"
或指定參數,如 VaryByParam="PcacheTime",VaryByParam="PcacheTime;page"--%>

(2)自訂緩衝

protected void Page_Load(object sender, EventArgs e)        {            if (Cache["myDog"] != null)            {                string str = Cache["myDog"].ToString();                Response.Write(str);            }            else            {                //1.設定永久 快取資料               // Cache["myDog"] = "我的小狗叫 花花 ~~~";                //2.設定 相對到期時間為10秒的 快取資料,如果一直有請求,就在當前的請求上重新延遲10秒                //Cache.Add("myDog", "我的小狗叫 花花 ~~~", null, System.Web.Caching.Cache.NoAbsoluteExpiration,                //    new TimeSpan(0, 0, 10), System.Web.Caching.CacheItemPriority.Default, null);                //3.設定 絕對到期時間為10秒後的 快取資料,不管有沒有一直請求,都是在建立10秒後到期                //Cache.Add("myDog", "我的小狗叫 花花 ~~~", null, DateTime.Now.AddSeconds(10), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default,OnRemoveCallback);                //4.為 快取項目 添加 回呼函數(當緩衝被 銷毀的時候 執行),帶 【檔案快取依賴】                //CacheDependency cd = new CacheDependency(Request.MapPath("/data.txt"));                //Cache.Add("myDog", "我的小狗叫 花花 ~~~", cd, Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, OnRemoveCallback);                //5.緩衝依賴資料庫 具體代碼參考 http://adyhpq.blog.163.com/blog/static/3866700201082624615851/                SqlCacheDependency sqlDep = new SqlCacheDependency("LWord", "Users");                IList<LeaveWordBorad.MODEL.Users> list = new LeaveWordBorad.BLL.Users().GetList();                Cache.Add("myDog", list.Count, sqlDep, Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, OnRemoveCallback);                Response.Write("剛買了只狗~~~!");            }        }        /// <summary>        /// 緩衝到期之後執行的函數        /// </summary>        /// <param name="key"></param>        /// <param name="value"></param>        /// <param name="reason"></param>        private void OnRemoveCallback(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)        {            string path = Server.MapPath("/log.txt");            System.IO.File.AppendAllText(path, key + "=" + value + "reason=" + reason);        }

7.IHttpModule 過濾器

 public class MyHttpModule:IHttpModule    {    //每次請求的時候都會調用該方法,可以在這裡進行url重寫        public void Init(HttpApplication context)        {            context.BeginRequest += context_BeginRequest;        }        void context_BeginRequest(object sender, EventArgs e)        {            HttpContext context =( (HttpApplication) sender).Context;            context.Response.Write("哈哈"+DateTime.Now);        }        public void Dispose()        {                   }     }Web.config<system.webServer>  <modules>    <add name="myHttpModule" type="WebApplication2.MyHttpModule"/>  </modules></system.webServer>Global檔案:  protected void Application_BeginRequest(object sender, EventArgs e)        {        //可以在這裡進行url重寫            Response.Write("Application_BeginRequest哈哈");        }protected void Application_BeginRequest(object sender, EventArgs e)        {            string requestUrl = Request.RawUrl;///WebForm1/8            Response.Write(requestUrl);            string[] strUrlParts = requestUrl.Split(new char[1]{‘/‘},StringSplitOptions.RemoveEmptyEntries);            if (strUrlParts.Count()>0)            {                if (strUrlParts[0].Equals("WebForm1", StringComparison.InvariantCultureIgnoreCase))                {                    HttpContext.Current.RewritePath("/WebForm1.aspx?id=" + strUrlParts.Last());                }                            }        }

 

聯繫我們

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