ASP.NetÖÐ״̬±£´æ ÔÚASP±£´æһЩ¹²Ïí¡¢¸öÈË״̬£¬Ê¹ÓõÄÊÇApplication¡¢Session¡¢CookieÕâÈýÖÖ¡£ºÇºÇ¡£ÔÚASP.NetÖÐM$³ýÁ˼̳ÐApplicationµÈÍ⣬ÓÖ¸øÎÒÃÇеÄÒ»ÖÖ״̬±äÁ¿ÐÅÏ¢State£¬ËüÊÇÓÃÀ´±£´æWEB¿Ø¼þµÄ״̬µÄ±äÁ¿£¬ºÜÐÂÏÊ°É£¬Ò»¶¨Òª¿ìµãÖªµÀÀ²£º£© ²»ºÃÒâ˼£¬·Éµ¶»¹ÊÇ´ÓÔʼһ²½²½À´Ëµ°É¡£ÏÈ´ÓApplication½²Æð¡£(̨ÏÂһƬºðÉù)£¬Ã»Óа취£¬ÎªÁËÓиö½×ÌÝÐÔ£¬»¹ÊDz»Ó¦µ±¹ýÔç½Ó´¥Ð¶«¶«À²£º£© Application ÔÚASP.NetÖÐApplicationÓ÷¨ÓëASPÊÇÒ»ÑùµÄ£¬¼¸ºõÊÇûÓÐʲô˵µÄ£¬µ«ÊÇËü¶àÁËÁ½¸öÌرðÓÐÓõÄʼþ£¬Application_OnBeginRequestºÍApplication_OnEndRequest¡£ËûÃǵĺÍÔÀ´µÄApplication_OnStartºÍApplication_OnEndÒ»ÑùÊÇ·ÅÔÚglobalÎļþÖеÄ(×¢ÒâÕâ¸öÎļþÔÚASPÖÐÃû×ÖÊÇglobal.asa£¬ÔÚASP.NetÖÐÊÇglobal.asax)¡£ ×¢£ºÕâ¸öʼþ£¬Ð´²»Ð´OnÊÇÒ»ÑùµÄ¡£ÈçApplication_EndÓëApplication_OnEndÊÇÒ»ÑùµÄ Application_OnStartÊÇÔÚÕû¸öASP.NetÓ¦ÓÃÊ×Ïȱ»´¥·¢µÄʼþ£¬Ò²¾ÍÊÇÔÚÒ»¸öÐéÄâĿ¼ÖеÚÒ»¸öASP.Net³ÌÐòÖ´ÐÐʱ´¥·¢£¬Application_OnEnd¾ÍÕýºÃÏà·´£¬ÔÚÕû¸öÓ¦ÓÃֹͣʱ±»´¥·¢£¨Í¨³£·¢ÉúÔÚ·þÎñÆ÷±»ÖØÆô/¹Ø»úʱ)¡£Application_OnRequestStartºÍApplication_OnRequestEndÔòÊÇÔÚÿһ¸öASP.Net³ÌÐò±»ÇëÇóʱ¾Í·¢Éú£¬Ò²¾ÍÊÇ˵¿Í»§·ÃÎÊÒ»´ÎÒ»¸öASP.Net³ÌÐò£¬ÕâÁ½¸öʼþ¾Í»á±»´¥·¢¡£ÎÒÃÇ¿ÉÒÔ´ÓÏÂÃæµÄ³ÌÐò¿´µ½ËûµÄÓ¦ÓÃ.ÎÒÃÇÏȽ¨Á¢Ò»¸öglobal.asax,ÄÚÈÝÈçÏ£º <script language="C#" runat="server"> void Application_OnBeginRequest(Object sender, EventArgs E) { Response.Write("Request is Starting...<br>"); } void Application_OnEndRequest(Object sender, EventArgs E) { Response.Write("Request is Ending...<br>"); } </script> È»ºó½«Æä·Åµ½±¾ÐéÄâĿ¼µÄ¸ùĿ¼ÏÂ,È»ºóÎÒÃÇËæ±ã´ò¿ªÒ»¸öʲôaspxÎļþ£¬ÕâÀïÎÒÃÇ´ò¿ªnamed.aspxÎļþ£¬¿´¿´: ͼ15-1 ¿´µ½Ã»ÓУ¬Õâ¸öÒ³ÃæµÄÉÏÃæºÍÏÂÃæ³öÏÖÁËÎÒÃÇÔÚglobal.asaxÖж¨ÒåµÄÓï¾äRequest is Starting...ºÍRequest is Ending...Õâ¸ö²»ÊÇÎÒÃÇÔÚÕâ¸öÎļþÖжÀ¼ÓµÄ£¬ÎÒÃǽ«»áÔÙÈκÎÒ»¸öASP.NetÎļþÖп´µ½ËüµÄÓ°×Ó¡£ Session ASP.NetÖеÄSessionÊÇÎÒ¼û¹ý×îºÃµÄ´¦Àí£¬ÊÇÆäËü¶¯Ì¬ÍøÒ³¼¼Êõ²»ÄܱÈÄâµÄ¡£ASP.NetÖеÄSessionÔÙÒ²²»ÐèÒªCookiesµÄÖ§³Ö£¬Ò²¾ÍÊÇ˵Èç¹ûÓû§¹Ø±ÕÁËCookie£¬SessionµÄÖµÒ»ÑùÒ²¿ÉÒÔ±»±£´æ¡£ ²»¹ý£¬ÎÒÃÇÐèÒª¶Ôconfig.webÎļþ½øÐÐһЩÅäÖÆ£¬ÒòΪÔÚÆäÖÐÕÒµ½¹ØÓÚSessionµÄÉèÖÃÎı¾£¬Èç: <sessionstate cookieless="false" /> °Ñcookieless="false" ¸Ä³Écookieless="true" £¬ÄÇôÒÔºóSession¾Í²»´¢´æÔÚcookiesÖÐÁË£¬¶øÔÚ´¢´æÔÚURLÖС£ ͼ15-2 ÉÏÃæµÄºÚȦÖеĶ«¶«¾ÍÊÇ´¢´æµÄSessionÖµ£¬µ±È»ÕâÊDZ»±àÂëµÄ£¬ÎÒÃÇÎÞ·¨ÖªµÀËüµÄֱʵֵ¡£ Æäʵ£¬ÎÒ·¢ÏÖ£¬²»ÓøÄconfig.webÎļþ£¬¹Øµôcookiesºó£¬²»ÓÃURLÒ²ÕÕÑùÄÜ´«µÝsessionÖµ£¬ÕâÖÖÇé¿öÏÂSessionÊÇÈçºÎ±£ÖµµÄ£¬Õâ¸ö¾Í²»Ì«Çå³þÁË£º£© Session»¹¿É±£³Ö²»ËÀÖ®Êõ£¬¾ÍÊǵ±·þÎñÆ÷ÖØÆô£¬»¹ÊÇÄܱ£Ö¤SessionÖеÄÖµ²»±ä£¬²»¹ýÕâÓÖÒª¸Ä¶¯config.webÎļþ¡£Ò²ÊÇͨ¹ýÏÂÃæµÄÓï¾äÉ趨. <sessionstate inproc="false" server="localhost" port="42424" /> ÊDz»ÊÇ·¢ÏÖÕâÀïµÄSessionÊDz»ÊǺÜÇ¿´ó°¡£º£©£¬°Ñlocalhost¸Ä³ÉÄúÒªµÄÖ÷»ú£¬Session»¹ÄÜÔÚÁíÍâһ̨Ö÷»úÉϱ£³Ö¡£ |