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
Ïà¹ØÎĵµ£º
1. Get(¼´Ê¹ÓÃQueryStringÏÔʽ´«µÝ)
·½Ê½£ºÔÚurlºóÃæ¸ú²ÎÊý¡£
Ìص㣺¼òµ¥¡¢·½±ã¡£
ȱµã£º×Ö·û´®³¤¶È×Ϊ255¸ö×Ö·û£»Êý¾Ýй©ÔÚurlÖС£
ÊÊÓÃÊý¾Ý£º¼òµ¥¡¢ÉÙÁ¿¡¢¹Ø¼üµÄÊý¾Ý¡£
ÊÊÓ÷¶Î ......
1. Web.configÖÐÌí¼Ó:
<globalization requestEncoding="utf-8" responseEncoding="utf-8"/>
2. 頁ÃæÖÐÌí¼Ó:
<head>
<meta http-equiv="Content-Type" content="charset=utf-8&qu ......
ÍøÂç´òÓ¡¸ÅÊö
¡¡¡¡• B/S½á¹¹µ¼ÖÂÁËWebÓ¦ÓóÌÐòÖдòÓ¡µÄÌØÊâÐÔ¡£
¡¡¡¡• ³ÌÐòÔËÐÐÔÚä¯ÀÀÆ÷ÖУ¬´òÓ¡»úÔÚ±¾µØ£¬¶øÎļþÈ·¿ÉÄÜÔÚ·þÎñÆ÷ÉÏ£¬µ¼ÖÂÁË´òÓ¡¿ØÖƲ»ÊǺÜÁé»î¡£
¡¡¡¡• ¸ñʽÈçºÎ¿ØÖƺͶ¨ÖƵȣ¬ÊÇÎÒÃÇ¿ª·¢ÖпÉÄÜ»áÃæ¶ÔµÄÎÊÌâ¡£
¡¡¡¡´òÓ¡ÎĵµµÄÉú³É
¡¡¡¡• 1¡¢¿Í»§¶Ë½Å±¾·½Ê½
¡¡¡¡Ò»°ãÇé¿öÏ£¬Ö÷ÒªÊ ......
Õâ¶Îʱ¼ä,ÀÏ´óÔÚ¿´ÁËÍøÕ¾ºó,·¢ÏÖviewstateËù²úÉúµÄÒ»¶ÑÂÒÂ룬ÑÏÖØÓ°ÏìÁËÎÒÃÇÍøÕ¾http://www.xbcar.net Î÷²¿Æû³µÍøµÄ´ò¿ªËٶȺÍËÑË÷ÒýÇæµÄץȡ£®¾Í¸øÎÒÏÂÁ˵ÀÌúÁÈýÌìÄÚ±ØÐë½â¾ö£®ÕâϸÃÂÖµ½ÎÒÍ·´óÁË£¬
ÀýÈ磺
input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="dEV4cHJlc3Npb247UmVhZE9ub ......