在global.asax中
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Products","Products/{id}/{productname}",
"~/productdetails.aspx");
}
在products.aspx
<ahref='<%#string.Format("/Products/{0}/{1}",Eval("id"),Clean.CleanUrl(Eval("ProductName").ToString())) %>'><%# Eval("ProductName") %></a>
在productsdetails.aspx.cs
if (!IsPostBack)
{
if (RouteData.Values["id"] != null)
{
loadproduct();
}
}
void loadproduct()
{
select ............. where id= RouteData.Values["id"].ToString();
}
這是清潔級
public static class Clean
{
public static string CleanURL(this string kelime)
{
if (kelime == "" || kelime == null) { return ""; }
kelime = kelime.Replace("ş", "s");
kelime = kelime.Replace("Ş", "S");
kelime = kelime.Replace(".", "");
kelime = kelime.Replace(":", "");
kelime = kelime.Replace(";", "");
kelime = kelime.Replace(",", "");
kelime = kelime.Replace(" ", "-");
kelime = kelime.Replace("!", "");
kelime = kelime.Replace("(", "");
kelime = kelime.Replace(")", "");
kelime = kelime.Replace("'", "");
kelime = kelime.Replace("ğ", "g");
kelime = kelime.Replace("Ğ", "G");
kelime = kelime.Replace("ı", "i");
kelime = kelime.Replace("I", "i");
kelime = kelime.Replace("ç", "c");
kelime = kelime.Replace("ç", "C");
kelime = kelime.Replace("ö", "o");
kelime = kelime.Replace("Ö", "O");
kelime = kelime.Replace("ü", "u");
kelime = kelime.Replace("Ü", "U");
kelime = kelime.Replace("`", "");
kelime = kelime.Replace("=", "");
kelime = kelime.Replace("&", "");
kelime = kelime.Replace("%", "");
kelime = kelime.Replace("#", "");
kelime = kelime.Replace("<", "");
kelime = kelime.Replace(">", "");
kelime = kelime.Replace("*", "");
kelime = kelime.Replace("?", "");
kelime = kelime.Replace("+", "-");
kelime = kelime.Replace("\"", "-");
kelime = kelime.Replace("»", "-");
kelime = kelime.Replace("|", "-");
kelime = kelime.Replace("^", "");
return kelime;
}
}
謝謝,我知道如何註冊路由等。我試圖避免使用RouteData集合使用替換QueryString集合使用的所有煩瑣的工作。 –