2013-08-19 56 views
0

誰能請我提供的細節,我們怎樣才能實現自定義URL在asp.net自定義網址在asp.net重寫

我現在的URL重寫看起來象下面這樣:

www.domainname.com/News/default.aspx?newstitle=todays latest news 

現在我想重定向到以下網址:

www.domainname.com/News/todays-latest-news 

請告訴我我們如何才能達到同樣的效果。

+0

參考:\ http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module – Hariprasad

回答

1

添加這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(); 
    } 
} 
0

您可以使用URLRewriter.Net來達到此目的。這是很容易融入到asp.net項目,也是開源的。將urlRewriter.Net的dll文件添加到您的項目中,並在您的web.config文件中設置重寫規則。在使用ajax回發頁面時要小心。在原始網址,如果你得到ajax回發問題。