今天看到一個問題提出的有關在webForm 下使用System.we.Routing實現url重寫的設想,特花了些時間整理了下,將如下作為解答;
要明白如何使用url Routing先搞清楚以下問題:
什麼是URL Routing?
所謂URL Routing(URL路由),指的是在Web中,URL指向的不再是某個物理檔案,而是一個說明有關URL路由的字串,開發人員可以自訂該字串的格式。在預設情況下,URL Routing在ASP.NET應用程式中是可以直接使用的,但在ASP.NET網站上,需要做一些配置才能使用
為什麼要使用URL Routing?
在使用URL Routing前,我們的URL可能是http://www.website.com/Index.aspx?id=1,使用URL Routing後,我們的URL可以變成http://www.website.com/Index/1。修改後的URL更加友好,更有利於SEO。
URL Routing只能在MVC中才能使用嗎?
路由程式集(System.Web.Routing.dll)在.NET Framework V3.5 sp1中就包含了,而MVC是在之後才發布的。因此不能說URL Routing只能在MVC中才能使用。不過在MVC中增加了Routing在一些擴充方法(包含在System.Web.Mvc的RouteCollectionExtemsion類中),使用起來更加方便.
下面簡單介紹下如何在Web Form中使用URL Routing。
1. 添加對程式集System.Web.Abstractions.dll,System.Web.Routing.dll的引用.
2. 添加一個IRouteHandler的實作類別WebFormRouteHandler
代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using System.Web.Compilation;
using System.Web.UI;
/// <summary>
///WebFormRouteHandler 的摘要說明
/// </summary>
public class WebFormRouteHandler:IRouteHandler
{
/// <summary>
/// 具體呈現的頁面路徑
/// </summary>
public string VirtualPath
{
get;
private set;
}
public WebFormRouteHandler(string virtualpath)
{
this.VirtualPath = virtualpath;
}
#region IRouteHandler 成員
/// <summary>
/// 實現 IHttpHandler介面中的GetHttpHandler方法,
/// </summary>
/// <param name="requestContext"></param>
/// <returns></returns>
public IHttpHandler GetHttpHandler(RequestContext requestContext)
{
// 從ReqestContext對象中擷取URL參數,並把值寫到HttpContext的Items集合中供路由目標頁面使用
foreach (var urlParm in requestContext.RouteData.Values)
{
requestContext.HttpContext.Items[urlParm.Key] = urlParm.Value;
}
//得到執行個體化頁面對象
var page = BuildManager.CreateInstanceFromVirtualPath(VirtualPath, typeof(Page)) as IHttpHandler;
return page;
}
#endregion
}
3. 修改配置web.config檔案(必要的支援url Routing方法)
<httpModules>
<add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</httpModules>
4、 Global.asax頁面代碼如下(如果沒有則添加此檔案)
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Web.Routing" %>
<javascript runat="server">
public static void RegisterRoutes(RouteCollection routes)
{
//注意此處調用的是WebFormRouteHandler類
routes.Add("Named", new Route("NameIndex/{action}", new RouteValueDictionary { { "action", "NameIndex" } }, new WebFormRouteHandler("~/Named/Index.aspx")));
routes.Add("Numbers", new Route("NumberIndex/{action}", new RouteValueDictionary { { "action", "NumberIndex" } }, new WebFormRouteHandler("~/Numbers/Index.aspx")));
//添加驗證,要求action參數值必須是13個字元,若action參數長度不等於13個字元(酌情更改),則會得到“無法找到資源”的錯誤提示。
routes.Add("Validate",new Route("ValidateIndex/{action}",new RouteValueDictionary { { "action", "ValidateIndex" }},new RouteValueDictionary { { "action", @"\w{13}" } },new WebFormRouteHandler("~/Validate/Index.aspx")));
}
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
void Application_End(object sender, EventArgs e)
{
//在應用程式關閉時啟動並執行代碼
}
void Application_Error(object sender, EventArgs e)
{
//在出現未處理的錯誤時啟動並執行代碼
}
void Session_Start(object sender, EventArgs e)
{
//在新會話啟動時啟動並執行代碼
}
void Session_End(object sender, EventArgs e)
{
//在會話結束時啟動並執行代碼。
// 注意: 只有在 Web.config 檔案中的 sessionstate 模式設定為
// InProc 時,才會引發 Session_End 事件。如果會話模式
//設定為 StateServer 或 SQLServer,則不會引發該事件。
}
</script>
1、Named(檔案夾)
下麵包含一個檔案:Index.aspx
2、Numbers(檔案夾)
下麵包含一個檔案:Index.aspx
3、Validate(檔案夾)
下麵包含一個檔案:Index.aspx
現在要將項目的根目錄下的Default.aspx檔案稍作修改
添加如下內容:
<div><a href="NameIndex">Go To Named/Index</a></div><br />
<div><a href="NumberIndex">Go To Numbers/Index</a></div><br />
<div><a href="ValidateIndex">Go To ValidateIndex!</a></div>
所有的代碼都寫完了,下面進行調試,如果沒有問題,代表程式可以跑
最後運行程式,點擊Default.aspx頁面中的三個連結,看看效果
最後說明:程式中包含了參數的使用,參數的驗證等,
如果此程式同學們有什麼更好的建議,請說明,如果您認為哪裡有問題,請不要出口傷人,出來混,總是要還的
如果有需要,本人會上傳源碼,歡迎同學們踴躍參與討論;