在專案內新建一個 Global.asax
先import 以下兩個 namespace
1 2 |
" ]<%@ Import Namespace="System.Web.Routing" %> <%@ Import Namespace="System.Web.Compilation" %> |
在 Application_Start(object 加入註冊
void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時執行的程式碼
1 |
RegisterRoutes(RouteTable.Routes); |
}
新增 虎理轉址 class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
public static void RegisterRoutes(RouteCollection routes) { //EX:網址為 http://xx.xx.xx/od/12345678 , 12345678 為可置換的內容 , 會對應到不網頁 routes.Add(new Route("od/{gpo_no}", new OrderRouteHandler("~/order/order_look.aspx"))); } public class OrderRouteHandler : IRouteHandler { private string virtualPath; public OrderRouteHandler(string virtualPath) { this.virtualPath = virtualPath; } public IHttpHandler GetHttpHandler(RequestContext requestContext) { //origquerystr 透過httpcontext 取出 gpo_no 值應用 string origQueryStr = HttpContext.Current.Request.QueryString.ToString(); string queryStr = "?gpo_no=" + requestContext.RouteData.Values["gpo_no"]; if (!string.IsNullOrEmpty(origQueryStr)) queryStr += "&" + origQueryStr; HttpContext.Current.RewritePath(string.Concat(virtualPath, queryStr)); return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as Page; } } |
主要的功能是把原本 落落長的網址 http://xx.xx.xx/order/order_look.aspx?gpo_no=12345678
簡化為 http://x.xx.xx/od/12345678 以增加可讀性
參考網址 Jailu.NET
(Visited 52 times, 1 visits today)