[英]ASP.NET Routing with Web Forms
我讀過ASP.NET Routing ... Goodbye URL重寫? 和使用路由使用WebForms這些都是很棒的文章,但僅限於簡單的,說明性的,“hello world” - 復雜的例子。
是否有人以非平凡的方式使用ASP.NET路由與Web表單? 任何需要注意的問題? 性能問題? 進一步推薦閱讀我應該先看看我自己的實現?
編輯找到這些額外有用的URL:
不確定這是否是你的答案,但這可能會讓你朝着正確的方向前進,Scott Hanselman(MSFT)展示如何獲得ASP.NET WebForms,ASP.NET MVC和ASP.NET動態數據 - 哦和AJAX一起工作和諧。
.net 4.0和ASP.net路由的兩個非常有用的鏈接
一個如何在ASP.NET中使用路由的簡單示例
添加到default.aspx 3按鈕 -
protected void Button1_Click(object sender, EventArgs e) { Response.Redirect("Second.aspx"); } protected void Button2_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Pants"); } protected void Button3_Click(object sender, EventArgs e) { Response.Redirect("Third.aspx?Name=Shoes"); }
在第三頁上讀取查詢字符串
protected void Page_Load(object sender, EventArgs e) { Response.Write(Request.QueryString["Name"]); }
現在,如果您運行該程序,您將能夠導航到第二和第三個表單。 這就是以前的樣子。 我們來添加路由。
使用System.Web.Routing添加新項目Global.aspx;
protected void Application_Start(object sender, EventArgs e) { RegisterRoutes(RouteTable.Routes); } void RegisterRoutes(RouteCollection routes) { routes.MapPageRoute( "HomeRoute", "Home", "~/Default.aspx" ); routes.MapPageRoute( "SecondRoute", "Second", "~/Second.aspx" ); routes.MapPageRoute( "ThirdRoute", "Third/{Name}", "~/Third.aspx" ); }
在default.aspx中修改protected void Button1_Click(object sender,EventArgs e){// Response.Redirect(“Second.aspx”); Response.Redirect(GetRouteUrl(“SecondRoute”,null)); }
protected void Button2_Click(object sender, EventArgs e) { //Response.Redirect("Third.aspx?Name=Pants"); Response.Redirect(GetRouteUrl("ThirdRoute", new {Name = "Pants"})); } protected void Button3_Click(object sender, EventArgs e) { // Response.Redirect("Third.aspx?Name=Shoes"); Response.Redirect(GetRouteUrl("ThirdRoute", new { Name = "Shoes" })); }
修改third.aspx中的頁面加載
protected void Page_Load(object sender, EventArgs e) { //Response.Write(Request.QueryString["Name"]); Response.Write(RouteData.Values["Name"]); }
運行程序,請注意url看起來更干凈 - 其中沒有文件擴展名(Second.aspx變為第二個)
傳遞多個參數
使用以下代碼向default.aspx添加新按鈕:
protected void Button4_Click(object sender, EventArgs e) { Response.Redirect(GetRouteUrl("FourthRoute", new { Name = "Shoes" , Gender = "Male"})); }
將以下代碼添加到global.asax
routes.MapPageRoute( "FourthRoute", "Fourth/{Name}-{Gender}", "~/Fourth.aspx" );
使用以下頁面加載創建Fourth.aspx頁面:
protected void Page_Load(object sender, EventArgs e) { Response.Write("Name is: " + RouteData.Values["Name"] + " and Gender is " + RouteData.Values["Gender"]); }
前幾天我看到這個播客鏈接到ScottGu的博客,可能對你有用
您可以在以下文章中以簡單的方式找到URL Routing。 它提供信息,例如,在路由上發送請求,在目標頁面上檢索URL參數,設置參數的默認值。
Mike Ormond使用ASP.NET設置URL路由的分步指南非常出色( 獲得ASP.NET路由啟動和運行 - 權威指南 )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.