本文由国外网络上收集整理的一些内容加上作者自己的一些见解,为你提供可行性的办法。ASP.NET 4.0动态路由实现了SEO友好性支持。但是,实战中使用动态URL路由参数才是最常见的。本文从国外网络上查找到几种可行的方法,提供于此。
1. 路由定义:
--------------------------------
routes.MapPageRoute("section-page-id", "{section}/{page}/{id}", "~/{section}/{page}.aspx")
--------------------------------------------------------------------------------
2.在GridView控件中存在一个控件HyperLinkField,如下表达:
--------------------------------------------------------------------------------
<asp:HyperLinkField NavigateUrl="<%$RouteUrl:RouteName=section-page-id,section=Clients,page=Groups,id=5%>" Text="Groups" />
--------------------------------------------------------------------------------
没有问题,但是只可怜这个id不是动态的。
3.实现上述动态目的,有如下几种方法可以借鉴:
i.
--------------------------------------------------------------------------------
<asp:TemplateField><ItemTemplate><a href="<% #GetRouteUrl("section-page-id",new {.section="Clients", .page="Groups", .id=Eval("Id")})%>">test</a></ItemTemplate></asp:TemplateField>
--------------------------------------------------------------------------------
ii.
--------------------------------------------------------------------------------
<asp:HyperLinkField NavigateUrl="<%$RouteUrl:RouteName=section-page-id,section=Clients,page=Groups,id=Convert.Int32(Eval("ID"))%>" Text="Groups" />
--------------------------------------------------------------------------------
iii.use the OnRowDataBound event to set the NavigateUrl of your Hyperlink.
--------------------------------------------------------------------------------
var hyperLink = (HyperLink)e.Row.FindControl("HyperLink1");hyperLink.NavigateUrl = ""; //Set here.
--------------------------------------------------------------------------------
下面是本人编写的程序中的两个代码片断,供参考:
--------------------------------------------------------------------------------
<asp:GridView ID="gvQuestions" runat="server" AutoGenerateColumns="False"nrowdatabound="gvQuestions_RowDataBound"nrowdeleting="gvQuestions_RowDeleting"><Columns><asp:TemplateField HeaderText="Title"><EditItemTemplate><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></EditItemTemplate><ItemTemplate><asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl='<%# GetRouteUrl("GotoQuestionRoute", new {QuestionID = Eval("Id") }) %>'Text='<%# Eval("Title") %>'></asp:HyperLink></ItemTemplate></asp:TemplateField>
--------------------------------------------------------------------------------
<asp:ListView ID="lvCategoryList" runat="server" DataSourceID="SqlDataSourceCategory">
<ItemTemplate>
<li style="background-color: #E0FFFF;color: #333333;">
<a id="A1"
href="<%= ResolveUrl("~/Category/")%><%#Eval("CategoryName")%>"> <%# Eval("CategoryName")%></a>
<br />
</li>
</ItemTemplate>
</asp:ListView>
--------------------------------------------------------------------------------