誰能請我提供的細節,我們怎樣才能實現自定義URL在asp.net
自定義網址在asp.net重寫
我現在的URL重寫看起來象下面這樣:
www.domainname.com/News/default.aspx?newstitle=todays latest news
現在我想重定向到以下網址:
www.domainname.com/News/todays-latest-news
請告訴我我們如何才能達到同樣的效果。
誰能請我提供的細節,我們怎樣才能實現自定義URL在asp.net
自定義網址在asp.net重寫
我現在的URL重寫看起來象下面這樣:
www.domainname.com/News/default.aspx?newstitle=todays latest news
現在我想重定向到以下網址:
www.domainname.com/News/todays-latest-news
請告訴我我們如何才能達到同樣的效果。
添加這Global.asax中
using System.Web.Routing; //top of the page
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "news/{news}", "~/news/default.aspx");
}
然後你就可以得到像下面Default.aspx中的新聞標題:
protected void Page_Load(object sender, EventArgs e)
{
if (this.RouteData.Values.Count > 0)
{
string newstitle = this.RouteData.Values[0].ToString();
}
}
實現你的任務中使用所謂的asp.net概念路由, 這裏是幾個例子參考爲更好地理解
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx
您可以使用URLRewriter.Net來達到此目的。這是很容易融入到asp.net項目,也是開源的。將urlRewriter.Net的dll文件添加到您的項目中,並在您的web.config文件中設置重寫規則。在使用ajax回發頁面時要小心。在原始網址,如果你得到ajax回發問題。
參考:\ http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module – Hariprasad