Asp.Net 4.0 SEO增强之 UrlRouting
在.Net 4.0之前我们为了做出搜索引擎友好的,对用户也友好的url都是需要自己实现Url重写,现在不需要了,.Net 4.0为我们做这一切。UrlRouting之所以称之为Routing是因为它不但实现了Url重写还可以通过参数得到重写后的Url在页面上使用。
1. Url Routing 的通常用法
UrlRouting在Asp.Net Mvc项目中被广泛使用,在Mvc中很好用,所以移植到了webform中,我们先看下在webform中的使用方式
假定一个使用场景:我们需要做博客每日文章的页面,我们希望的url地址是:
/archive/2010/05/10/default.aspx
这个地址将被映射到~/posts.aspx文件上
要使用UrlRouting,需要将UrlRouting的规则注册到RouteTable中,如下Global文件中注册Routing规则的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | public static void RegisterRoutes(RouteCollection routes) { routes.Ignore( "{resource}.axd/{*pathInfo}" ); routes.MapPageRoute( "blogs" , //给这个UrlRouting规则起一个名字 "archive/{year}/{month}/{date}/default.aspx" , //希望的友好Url地址格式 "~/blogs.aspx" , //映射到的aspx页面路径 false , //是否需要检查用户权限 new RouteValueDictionary{ { "year" , DateTime.Now.Year }, { "month" , DateTime.Now.Month }, { "date" , DateTime.Now.Date} }, //参数的默认值 new RouteValueDictionary { { "year" , @"(19|20)\d{2}" }, { "month" , @"\d{1,2}" }, { "date" , @"\d{1,2}" } } //参数的规则,我们在这里限制url中的年月日是我们想要的数据格式 ); } void Application_Start( object sender, EventArgs e) { //在Application_Start时注册的Routing规则 RegisterRoutes(RouteTable.Routes); } |
2. 在页面中使用UrlRouting参数值
1) 在后台代码中使用Route的值
1 2 3 4 5 6 | protected void Page_Load( object sender, EventArgs e) { string year = ( string )RouteData.Values[ "year" ]; string month = ( string )RouteData.Values[ "month" ]; string date = ( string )RouteData.Values[ "date" ]; } |
2) 在页面上使用
1 2 3 | < asp:Literal ID="literalYear" runat="server" Text="<%$RouteValue:year %>"></ asp:Literal > -< asp:Literal ID="literal1" runat="server" Text="<%$RouteValue:month %>"></ asp:Literal > -< asp:Literal ID="literal2" runat="server" Text="<%$RouteValue:date %>"></ asp:Literal > |
3) 在DataSource中使用RouteParameter
1 2 3 4 5 6 | < asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TestDb %>" SelectCommand="SELECT BlogID,BlogTitle FROM Blogs Where Category = @category"> < SelectParameters > < asp:RouteParameter Name="category" RouteKey="category" /> </ SelectParameters > </ asp:SqlDataSource > |
4) 在页面上显示RouteUrl
1 | < a href='<%=GetRouteUrl("blogs",new {year=2010,month=05,date=05}) %>'>2010年5月1日的博客</ a > |
3. UrlRouting和UrlRewrite的区别
UrlRouting相对于Url重写是一个比较新的事物,UrlRouting的长处是可以做双向转换,既可以做url重写,还可以根据一些参数获得重写后的Url地址,但是它也有自己的不足之处,比如说如果你想连域名一起重写,比如博客地址yukaizhao.cnblogs.com这样的重写,UrlRouting就做不到了,只能用UrlRewrite。
Asp.net 新特性相关阅读:
1. 从页面标记<%%>说起
2. Asp.Net 4.0 中可以用自定义的Provider做OutputCache 了
3. SEO增强支持MetaKeywords,和MetaDescription,RedirectPermanant
4. SEO增强之URL Routing
5. 输出更纯净的Html代码,ViewStateMode和ClientIDMode,CheckBoxList等
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2009-05-20 预防甲型H1N1流感(猪流感)个人防护指南
2008-05-20 在微软中文技术论坛 CSDN cnblogs 三个微软社区中提问