context.Server.TransferÊÇÔÚ·þÎñÆ÷ÄÚ²¿°ÑÇëÇó´¦ÀíȨת½»¸ø"PersonsView.aspx"´¦Àí¡£HttpContextÖеÄItemsµÄÉúÃüÖÜÆÚÊÇÕû¸öÇëÇóÏìÓ¦£¬ÕâÑùÎÒÃÇÔÚÒ»°ã´¦Àí³ÌÐòÖаÑÊý¾Ý·Åµ½context.ItemsÖУ¬È»ºóÔÚ"PersonsView.aspx"ÖоͿÉÒÔ¿ÉÒÔͨ¹ýContext.Items["persons"]Äõ½Õâ¸öÊý¾Ý´Ó¶ø½øÐÐÊý¾ÝµÄչʾ¡£
Ϊʲô°ÑÊý¾Ý·Åµ½HttpContext.ItemsÖÐÄØ¡£ÓÉÓÚTransfer½ö½öÊÇ·þÎñÆ÷ÄÚ²¿´¦ÀíȨµÄת½Ó£¬µ«ÊÇÈÔÈ»ÊÇÔÚÒ»¸öÇëÇóÖеģ¬ËùÒԷŵ½Context.itemsÊÇ×îºÃµÄ¡£Èç¹û·Åµ½Session¡¢ApplicationµÈÖлáÓв¢·¢µÄÎÊÌâ¡£
ÕâÑùController£¨Ò»°ã´¦Àí³ÌÐò£©ºÍView£¨aspx£©Ö®¼äֻҪά³ÖÒ»¸ö“Òª´«µÝÒ»¸öÃû×ÖΪpersonsÀàÐ͵ÄList<Person>”ÕâÑùÒ»¸öÈõñîºÏ¹Øϵ¼´¿É£¬ÖÁÓÚaspxÓò»ÓÃÕâ¸öpersons¡¢Óü¸´Îpersons¡¢ÔõôÓÃpersons£¬ÄãController¶¼²»Óùܡ£
°´ÕÕͬÑù·½·¨¸ÄÔì¼Ó·¨¼ÆËãÆ÷
ÏÂÃæÊÇÊÓͼAddView.aspxµÄ´úÂë
¼Ó·¨¼ÆËãÆ÷+
¶øAddHandler.ashxµÄÖ÷Ìå´úÂëÈçÏ£º
public void ProcessRequest(HttpContext context) { ]; dynamic viewBag = new System.Dynamic.ExpandoObject(); if(string.IsNullOrEmpty(action)) { viewBag.i1 = ""; viewBag.i2 = ""; viewBag.i3 = ""; } ) { ]); ]); int i3 = i1 + i2; viewBag.i1 = i1; viewBag.i2 = i2; viewBag.i3 = i3; } context.Items[] = viewBag; context.Server.Transfer(); }
ÓÉÓÚViewºÍControllerÖ®¼ä´«µÝµÄÊý¾Ý±È½Ï¸´ÔÓ¡¢±È½Ï¶à£¬ÎªÁ˼ò»¯¿ª·¢£¬ÕâÑùÓÃÁËdynamic ¶¯Ì¬ÀàÐÍ¡£ÊìϤASP.Net MVCµÄͬѧÊDz»ÊǸоõÕâ¸öºÍASP.net MVCÖеÄViewBagÒìÇúͬ¹¤ÄØ¡£
ÔÚÕâ¸ö¼Ó·¨¼ÆËãÆ÷ÖÐÈç¹ûÎÒÏë°Ñ½áËã½á¹ûÓÃspanÏÔʾ£¬ÄÇôֻҪ°Ñ<input type="text" name="i3" value="<%=viewBag.i3%>" />¸Ä³É<span><%=viewBag.i3%></span>¾Í¿ÉÒÔÁË¡£ÃÀ¹¤Ò²ÈÝÒ×½éÈëÒ³ÃæÃÀ»¯¡£
Èç¹ûÎÒÏë°ÑÒ³ÃæµÄTitle´Ó“¼Ó·¨¼ÆËãÆ÷”¸Ä³É“3ºÍ5Ïà¼ÓµÄ½á¹û”£¬ÄÇôֻҪÕâÑù¸Ä¾Í¿ÉÒÔ£º<title><%=viewBag.i1 %>ºÍ<%=viewBag.i2 %>Ïà¼ÓµÄ½á¹û</title> ÕâÑùmodelÀïµÄÊý¾ÝÎÒaspxÏëÓü¸´ÎÓü¸´Î£¬ÏëÔõôÓþÍÔõôÓá£ÓÐûÓиоõµ½ºÍWebForm²»Ò»ÑùµÄµØ·½ÄØ£¿
ËùÒÔֻҪ˼ÏëÏëͨÁË£¬ÆäʵʵÏÖMVCģʽ²»Ò»¶¨ÒªÓÃASP.net MVC£¬ÎÒ¼û¹ýºÜ¶àÏîÄ¿¶¼ÊÇ×Ô¼º¸ãµÄMVC»úÖÆ¡£
µ±È»¼ÈÈ»ASP.net MVCÒѾÕâôÓÅÐãÁË£¬Ò»°ãÇé¿öû±ØÒªÏñÎÒÕâÑùÖØÐÂ×Ô¼º·¢Ã÷Ò»¸öÂÖ×ÓÓá£
ÕâÀï¸ø´ó¼Ò½²ASPXʵÏÖMVCÖ»ÊÇÓôó¼ÒÊìϤµÄ¶«Î÷Èôó¼ÒÃ÷°×ÔÀí£¬¹¤³ÌÏîÄ¿Ó¦Óû¹ÊÇÖ±½ÓÓÃASP.Net MVC°É¡£
¡¡