设为首页收藏本站language 语言切换
查看: 1475|回复: 0
收起左侧

ASP.NET Web Forms 4.0中的双向Routing支持

[复制链接]
发表于 2010-2-25 10:50:07 | 显示全部楼层 |阅读模式
<p >.NET Framework 3.5 SP1已经包含了ASP.NET Routing引擎。现在微软已经在ASP.NET WebForms 4.0中增加了对Routing引擎更好的支持,它使用表达式构造器进行双向Routing。 <>  Channel 9刚发布了一个10-4的新视频,展示如何在ASP.NET 4.0中使用这个新功能。</P><>  下面是视频中展示的一段代码。他们使用了一个经典示例,展示如何将Product.aspx?category=Jerseys映射至Product/Jerseys。在使用ASP.NET Routing引擎时,我们可以在Application_Start中向RouteTable添加这样的映射:</P><DIV ><DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN >RouteTable.Routes.Add(</SPAN><SPAN >"</SPAN><SPAN >roduct</SPAN><SPAN >"</SPAN><SPAN >,<BR></SPAN><SPAN >new</SPAN><SPAN > Route(</SPAN><SPAN >"</SPAN><SPAN >roduct/{name}</SPAN><SPAN >"</SPAN><SPAN >, <BR></SPAN><SPAN >new</SPAN><SPAN > PageRouteHandler(</SPAN><SPAN >"</SPAN><SPAN >~/Product.aspx</SPAN><SPAN >"</SPAN><SPAN >)));</SPAN></DIV></DIV><>  目前为了得到双向的Routing支持,用户必须对Query String进行URL重写。不过,使用ASP.NET 4.0时,用户可以注册如下的表达式构造器(expression builder):</P><DIV ><DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN ><</SPAN><SPAN >system.web</SPAN><SPAN >></SPAN><SPAN ><BR>  </SPAN><SPAN ><</SPAN><SPAN >compilation</SPAN><SPAN >></SPAN><SPAN ><BR>    </SPAN><SPAN ><</SPAN><SPAN >expressionBuilders </SPAN><SPAN >...</SPAN><SPAN >></SPAN><SPAN ><BR>      </SPAN><SPAN ><</SPAN><SPAN >add </SPAN><SPAN >expressionPrefix</SPAN><SPAN >="RouteUrl"</SPAN><SPAN ><BR>           type</SPAN><SPAN >="System.Web.Compilation.RouteUrlExpressionBuilder"</SPAN><SPAN ></SPAN><SPAN >/></SPAN><SPAN ><BR>      </SPAN><SPAN ><</SPAN><SPAN >add </SPAN><SPAN >expressionPrefix</SPAN><SPAN >="RouteValue"</SPAN><SPAN ><BR>           type</SPAN><SPAN >="System.Web.Compilation.RouteValueExpressionBuilder"</SPAN><SPAN ></SPAN><SPAN >/></SPAN><SPAN ><BR>    </SPAN><SPAN ></</SPAN><SPAN >expressionBuilders</SPAN><SPAN >></SPAN><SPAN ><BR>  </SPAN><SPAN ></</SPAN><SPAN >compilation</SPAN><SPAN >></SPAN><SPAN ><BR></SPAN><SPAN ></</SPAN><SPAN >system.web</SPAN><SPAN >></SPAN></DIV></DIV><> 第一个表达式用于生成URL而第二个用于获取Route值。在aspx页面中可以使用$符号来访问表达式:</P><DIV ><DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN ><</SPAN><SPAN >asp:HyperLink </SPAN><SPAN >NavigationUrl</SPAN><SPAN >="<%$ RouteUrl:RouteName=Product, name=Jerseys"</SPAN><SPAN ><BR>     Text</SPAN><SPAN >="Jerseys"</SPAN><SPAN ><BR>     runat</SPAN><SPAN >="server"</SPAN><SPAN ></SPAN><SPAN >/></SPAN></DIV></DIV><>  如果要获取name属性中的值,用户可以使用Route对象而不是Request对象:</P><DIV ><DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN >RouteData.Values[</SPAN><SPAN >"</SPAN><SPAN >name</SPAN><SPAN >"</SPAN><SPAN >];</SPAN></DIV></DIV><>   或使用表达式构造器:</P><DIV ><DIV><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN ><%</SPAN><SPAN >$ RouteValue:name </SPAN><SPAN >%></SPAN></DIV></DIV><>  用户可以利用ASP.NET Routing引擎和新的双向支持将URL和物理上的Web Form解耦,以便支持对搜索引擎更友好的URL。</P><p align="center"></p></p>
您需要登录后才可以回帖 登录 | 论坛注册

本版积分规则

QQ|Archiver|手机版|小黑屋|sitemap|鸿鹄论坛 ( 京ICP备14027439号 )  

GMT+8, 2025-4-25 20:20 , Processed in 0.077504 second(s), 24 queries , Redis On.  

  Powered by Discuz!

  © 2001-2025 HH010.COM

快速回复 返回顶部 返回列表