ASP.NET 4µ±ÖÐUrl RoutingµÄÖ§Ô®
ASP.NET 4µÄWeb Formsµ±ÖУ¬×îÁîÈ˼¤É͵ģ¬ÔòĪ¹ýÓÚÊÇURL Routing»úÖƵÄÈ«ÃæÖ§³Ö¡£¹ýÈ¥ÔÚASP.NET 3.5 SP1µ±ÖУ¬Web Forms»ò¶à»òÉپͿªÊ¼Ö§³ÖURL Routing»úÖÆ£¬ËüÈÃÎÒÃÇÔÚÍøÖ·µÄ³ÊÏÖÒÔ¼°Ê¹ÓÃÉϸü¼ÓµÄÓе¯ÐÔ¡£
¹ýÈ¥ÎÒÃÇÔÚASP.NETµ±ÖУ¬Ï°¹ßÓÚµ×ÏÂÕâÑùµÄÍøÖ·³ÊÏÖ·½Ê½£º
http://myWebSite/EditProduct.aspx?Id=1
µ«×î½ü¼¸ÄêREST·ç¸ñµÄÍøÕ¾(»òREST Web·þÎñ)ÐËÆð£¬Äã³£³£»á¿´µ½ÍøÖ·µÄ³ÊÏÖ·½Ê½±ä³É£º
http://myWebSite/Product/Edit/XBOX
ÉÏÃæÕâÑùµÄÍøÖ·ÓÐһЩºÃ´¦£¬Ê×ÏÈ£¬ÍøÖ·¿ÉÒÔ¸üÃ÷È·µÄ±í´ïÏëÒª³ÊÏֵŦÄÜ£¬»òÊÇÒªºó¶ËÓ¦ÓóÌÐò½øÐеÄÐÐΪ£»Æä´Î£¬ÉÏÃæÕâÑùµÄÍøÖ·ÓÉÓÚ²¢·Ç¶ÔÓ¦µ½ºó¶Ëijһ¸öʵÌåµµ°¸(.aspx.cs)£¬¶øÊÇ͸¹ýRouting»úÖÆÀ´×ªÅÉ£¬Òò´ËÏà½Ï¹ýÈ¥µÄÍøÖ·ÓÐןü¸ßµÄ°²È«ÐÔ¡£
Ò²¾ÍÊÇ˵£¬ÔÚÕâÑùµÄ¼Ü¹¹Ï£¬ASP.NETÓ¦ÓóÌÐòµÄÍøÖ·ÔÙÒ²²»Ö»ÊǶÔÓ¦µ½ÊµÌåµµ°¸µÄ·¾¶£¬¶øÊÇ¿ÉÓÃÀ´±í´ïÒªÖ´ÐеŦÄÜ¡£ÖÁÓÚʵ¼ÊÖ´ÐÐʱҪ´¦ÀíµÄ³ÌÐò´úÂë»ò³ÊÏÖ½á¹ûµÄÍøÒ³£¬Ò²²¢·ÇÒ»¶¨ÒªÊÇÓÉÍøÖ·ËùÖ¸ÏòµÄʵÌåÒ³Ãæ¡£ÕâÒ²ÈÃÎÒÃÇÔÚ¿ª·¢Ó¦ÓóÌÐòʱÓиü´óµÄµ¯ÐÔ£¬ÀýÈçÒ»°ãµÄ²¿Âä¸ñÍøÕ¾ÍøÖ·¿ÉÄÜÊÇ¡ºhttp://blog/studyhost¡»£¬ÓÉÓÚblogÍøÕ¾¶à°ë¶¼¿ÉÒÔ¿ª·Å¸ø¶àÈËÉêÇ룬ÀíËùµ±È»µÄÿһ¸öÓû§¶¼ÓÐÀàËÆ¡ºhttp://blog/ÉêÇëÕßID¡»ÕâÑùµÄÍøÖ·¡£µ±È»£¬¶ÔÓ¦µ½ASP.NETµÄºó¶ËÓ¦¸Ã¶¼ÊÇͬһÌ׳ÌÐòÀ´´¦Àí£¬ÔÚ¹ýÈ¥µÄASP.NETÓ¦ÓóÌÐòµ±ÖУ¬ÎÒÃǵÃҪɷ·ÑÒ»·¬¹¤·ò²ÅÄÜÈÃÍøÕ¾¿ÉÒÔ½ÓÊÜÕâÑùµÄÍøÖ·Éè¼Æ(°Ñ²ÎÊý´ÓQueryStringת±ä³ÉREST·ç¸ñ)£¬¶øÏÖÔÚ£¬Í¸¹ýURL Routing»úÖƺܿìµØ¾ÍÄÜÇáËɸ㶨¡£
³ý´ËÖ®Í⣬»¹ÓÐÁíÒ»¸öÏÔ¶øÒ×¼ûµÄºÃ´¦£¬²ÉÓÃREST·ç¸ñµÄÍøÖ·£º
http://myWebSite/Product/Edit/XBOX
vs.
http://myWebSite/EditProduct.aspx?Id=1
Ïà½ÏÓÚ´«Í³ÍøÖ·¸üÈÝÒ×±»GoogleµÈËÑË÷ÒýÇæ²éѯÓë¼ìË÷£¬±Ï¾¹ÉÏͼÍøÖ·ÖеÄXBOX±ÈÆð²úÆ·Id=1À´µÃÈÝÒ×Àí½âµÄ¶à¡£
ÄÇÎÒÃÇÒªÈçºÎÔÚASP.NET 4µ±ÖÐʹÓÃÕâÑùµÄ»úÖÆÄØ£¬ÄúÖ»ÐèҪ͸¹ýмÓÈëµÄRoutesÀà±ð£¬ÀûÓÃMapPageRoute·½·¨¼´¿ÉÇáÒ×µÄÍê³ÉURL·ÓɵÄÖ¸¶¨£¬ÀýÈ磺
protected void Application_Start(object sender, EventArgs e) {
RouteTable.Routes.MapPageRoute(
"TestRoute", "Search/{ProductName}", "~/WebForm1.aspx");
}
ÔÚGlobal.asaxÈç´Ë׫д֮ºó£¬µ±Ê¹ÓÃÕßÔÚÍøÖ·ÁмüÈ룺
http://localhost:1031/Search/AK47
ÍøÒ³(Ó¦ÓóÌÐòÖ÷¿ØȨ)½«±»µ¼Òýµ½WebForm1.aspxÒ³Ã棬¶øÔÚ¸ÃÒ³ÃæÖÐÔò¿ÉÒÔ͸¹ýµ×ϵķ½Ê½À´È¡µÃ²ÎÊýProductN
Ïà¹ØÎĵµ£º
ASP.NETÒ³ÃæÖ®¼ä´«µÝÖµµÄ¼¸ÖÖ·½Ê½?
Ò»°ãÀ´½²ÓÐÒÔϼ¸ÖÖ£ºQueryString,session,cookies,application,server.Transfer
1. ʹÓÃQueryString±äÁ¿
QueryStringÊÇÒ»Öַdz£¼òµ¥µÄ´«Öµ·½Ê½£¬Ëû¿ÉÒÔ½«´«Ë͵ÄÖµÏÔʾÔÚä¯ÀÀÆ÷µÄµØÖ·À¸ÖС£Èç¹ûÊÇ´«µÝÒ»¸ö»ò¶à¸ö°²È«ÐÔÒªÇ󲻸߻òÊǽṹ¼òµ¥µÄÊýֵʱ£¬¿ÉÒÔʹÓÃÕâ¸ö·½·¨¡£µ«ÊǶÔÓÚ´«µÝÊý ......
1.ÖØÔØprotected override bool OnBubbleEvent(object source, EventArgs args)
OnBubbleEventÊǿؼþÄÚ°üº¬µÄ¿Ø¼þÏòÍâ²ã¿Ø¼þ×÷ʼþðÅÝ
-------------------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Co ......
MainPageÎļþ£ºWebForm1.aspx
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false" Inherits="TestMasterPage.WebForm1" enableViewState="False"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML> ......
Ê×ÏÈÃèÊöÒ»ÏÂÎÊÌâ:ÒòΪ¿Í»§µÄÐèÒª,°ÑËùÓжÔÓÚÊý¾Ý¿âµÄ²Ù×÷×ö³ÉÁË´æ´¢¹ý³Ì,ͬʱҲ´æÔÚ´øÓÐÊäÈë²ÎÊýµÄ´æ´¢¹ý³Ì.¿Í»§ÒªÇó½«ËùÓеÄÐÅÏ¢¶¼¿ÉÒÔÓÃË®¾§±¨±íÏÔʾ.ÎÒÃǶ¼ÖªµÀ,ҪʹÓÃË®¾§±¨±íÎÒÃǾÍҪʹÓÃÊý¾ÝÔ´µÄ.ÔÚÕâÀï,ÎÒÃÇʹÓõÄÊý¾ÝÔ´×ÔÈ»¶øȻҲ¾ÍÊÇ´æ´¢¹ý³ÌÁË.µ±È»,ÎÒÃÇÒ²ÊÇÖªµÀµÄ,ÔÚADO.NETµÄÁ ......
HTML´úÂ룺
<script language="javascript" event="onclick" for="ImageButton1">
return confirm('È·¶¨Òª±£´æ±ä¸üÂð£¿');//·µ»Øfalseʱ²»Ö´ÐÐImageButton1_Click·½·¨£¬·µ»ØtureʱִÐÐImageButton1_Click·½·¨
</script>
<asp:ImageButton ID="ImageButton1" runat="server" ImageAlig ......