. NET Learning Master page execution order, JSONP cross-domain request principle, IsPostBack principle, server-side control button Click Process, cache, IHttpModule filter

Source: Internet
Author: User
Tags httpcontext

1.WebForm the order in which the master page is executed is to execute the Page_Load in the sub-page before executing the Page_Load in the master page, the request is the control tree of the master page, and the control tree of the child page is populated to the master page, and the final output

2.WebForm authentication when using master pages for login

//Create a new page parent class Public Abstract classBasepage:page {protected voidPage_Load (Objectsender, EventArgs e) {            //1. Method of authentication Session            if(session["Uinfo"] ==NULL)            {                if(request.cookies["Uinfo"] ==NULL) {pagehelper.writejsmsg ("you are not logged in ~ ~","/manage/view/login.html");                Response.End (); }                Else                {                    //If there is a user ID in the cookie, the user object is read by ID and stored in Session                    stringStrid = request.cookies["Uinfo"].                    Value; //Verify that Strid is an integral type//validation succeeds reading the database and saving the user object to the sessionsession["Uinfo"] =NewBll. Users (). Getmodel (int.                    Parse (Strid)); //2. Overriding methods for calling subclasseschildpageload (); }            }                  }         Public Abstract voidchildpageload (); }//pages that require permission validation are inherited BasePage Public Partial classWebform3:basepage { Public Override voidchildpageload () {Throw Newnotimplementedexception (); }    }

3.JSONP cross-domain request principle is to use the script tag can cross-domain, request back the string browser as JS code to execute
Code for jquery requests:

$.ajax ("URL", type:"Get", DataType:"Jsonp", Jsonp:"Callback",//callback function Name parameter of the sending serverJsonpcallback:"Callbackfun",//Specifies the function name of the callbacksuccess:function () {alert ("Request succeeded"); }); function Callbackfun (data) {}

4.IsPostBack principle:
Whether to submit the form, the server-side ASP. NET Framework relies on the _viewstate in the detection request message to set the value

5. Server-side Control button click on the process
The 8th event creates the foreground page object, calls the page's Proccessrequest method between 11 and 12, builds the page control tree, calls Pageload, handles non-press
Click events, handle button click events, render generated HTML code

When a button is clicked on the browser side, a request message is generated to the server, the name of the clicked button is uploaded to the server, and the server-side ASP.
Depending on the name, the corresponding server control is found in the control tree and the event is executed to invoke the event method, but if the request message contains more than one
button name, the server side only executes the last button's event method.

6. Caching
(1) ASP. NET page Output cache
<%@ OutputCache duration= "varybyparam=" Id;name "%>"
<%--varybyparam Required property, can be varybyparam= "none" or varybyparam= "*"
Or specify parameters, such as varybyparam= "Pcachetime", varybyparam= "Pcachetime;page"--%>

(2) Custom cache

protected voidPage_Load (Objectsender, EventArgs e) {            if(cache["Mydog"] !=NULL)            {                stringstr = cache["Mydog"].                ToString ();            Response.Write (str); }            Else            {                //1. Set Persistent cache data//cache["Mydog"] = "My puppy is called Flower ~ ~ ~"; //2. Set cache data with a relative expiration time of 10 seconds and, if requested, re-defer 10 seconds on the current request//Cache.Add ("Mydog", "My puppy's name is flower ~ ~ ~", NULL, System.Web.Caching.Cache.NoAbsoluteExpiration,//New TimeSpan (0, 0, ten), System.Web.Caching.CacheItemPriority.Default, NULL); //3. Set the absolute expiration time to 10 seconds after the cached data, regardless of whether there has been a request, is created 10 seconds after the expiration//Cache.Add ("Mydog", "My puppy's name is flower ~ ~ ~", NULL, DateTime.Now.AddSeconds (10),                System.Web.Caching.Cache.NoSlidingExpiration, System.web.caching.cacheitempriority.default,onremovecallback); //4. Add a callback function for the cache entry (executed when the cache is destroyed) with "file cache dependency"//cachedependency cd = new CacheDependency (Request.mappath ("/data.txt")); //Cache.Add ("Mydog", "My Puppy named Flower ~ ~ ~", CD, Cache.noabsoluteexpiration, System.Web.Caching.Cache.NoSlidingExpiration,                System.Web.Caching.CacheItemPriority.Default, onRemoveCallback); //5. Cache dependent database specific code referencehttp://adyhpq.blog.163.com/blog/static/3866700201082624615851/SqlCacheDependency SQLDEP =NewSqlCacheDependency ("Lword","Users"); IList<LeaveWordBorad.MODEL.Users> list =NewLeaveWordBorad.BLL.Users ().                GetList (); Cache.Add ("Mydog", List. Count, SQLDEP, Cache.noabsoluteexpiration, System.Web.Caching.Cache.NoSlidingExpiration,                System.Web.Caching.CacheItemPriority.Default, onRemoveCallback); Response.Write ("just bought a dog, ~~~!. "); }        }        /// <summary>        ///functions that are executed after the cache expires/// </summary>        /// <param name= "key" ></param>        /// <param name= "value" ></param>        /// <param name= "Reason" ></param>        Private voidonRemoveCallback (stringKeyObjectvalue, System.Web.Caching.CacheItemRemovedReason reason) {            stringPath = Server.MapPath ("/log.txt"); System.IO.File.AppendAllText (Path, key+"="+ Value +"reason="+reason); }

7.IHttpModule Filter

  Public classMyhttpmodule:ihttpmodule {//This method is called every time the request is made, and URL rewriting can be done here         Public voidInit (HttpApplication context) {context. BeginRequest+=context_beginrequest; }        voidContext_beginrequest (Objectsender, EventArgs e) {HttpContext context=((HttpApplication) sender).            Context; Context. Response.Write ("haha"+DateTime.Now); }         Public voidDispose () {}} Web. config<system.webServer> <modules> <add name="Myhttpmodule"Type="Webapplication2.myhttpmodule"/> </modules></system.webServer>Global file:protected voidApplication_BeginRequest (Objectsender, EventArgs e) {        //URL rewriting can be done hereResponse.Write ("application_beginrequest haha"); }protected voidApplication_BeginRequest (Objectsender, EventArgs e) {            stringRequesturl = Request.rawurl;///WEBFORM1/8Response.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 ()); }                            }        }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.