System.Web.Routing入門及進階_實用技巧

來源:互聯網
上載者:User
UrlRouting進階應用程式
預計效果:
複製代碼 代碼如下:

當我訪問/a/b.aspx時就會轉到Default.aspx?category=a&action=b在頁面上顯示
category:a
action:b
亦如果我訪問/chsword/xxxx.aspx就會轉到Default.aspx?category=chsword&action=xxxx就會顯示
category:chsword
action:xxxx
如果訪問/chsword/就會轉到 Default.aspx?category=chsword&action=index就會顯示
category:chsword
action:index

首先我建立一個Route
複製代碼 代碼如下:

routes.Add(
"Default",
new Route("{category}/{action}.aspx",
new RouteValueDictionary(
new
{
file = "Default",
category = "home",
action = "index"
}), new MyRouteHandler()
)
);

當然IHttpHandler的處理方式也要有所改變
為了方便查看我使用了下方法:
複製代碼 代碼如下:

context.Server.Execute(string.Format("/{0}.aspx?category={1}&action={2}",
RequestContext.RouteData.Values.ContainsKey("file")
? RequestContext.RouteData.Values["file"].ToString()
: "default",
RequestContext.RouteData.Values.ContainsKey("category")
? RequestContext.RouteData.Values["category"].ToString()
: "",
RequestContext.RouteData.Values.ContainsKey("action")
? RequestContext.RouteData.Values["action"].ToString()
: "")
);

即/a/b.aspx是映射到Default.aspx?category=a&action=b
在Default.aspx中寫如下代碼:
複製代碼 代碼如下:

category:<%=Request.Params["category"] %><br />
action:<%=Request.Params["action"] %>

以顯示傳入的參數。
如果在IIS中設定Index.aspx時就算輸入/a/也會訪問到/a/index.aspx,即預設的會按RouteValueDictionary中設定的值自動補全
UrlRouting使用Regex規則
UrlRouting在定義的時候也可以按正則的規則來進行定義。
複製代碼 代碼如下:

routes.Add(
"zz",
new Route("{category}/{action}.chs",
new RouteValueDictionary(
new {
file = "Default",
category = "home",
action = "1"
}),
new RouteValueDictionary(
new {
action = "[\\d]+"
}),
new MyRouteHandler()
)
);

以上代碼規定了action只能是數字則訪問/a/1.chs可以正常訪問。
而訪問/a/b.chs則會顯示未找到資源。
當然這是裡可以使用更進階的Regex。
UrlRouting的技巧
排除UrlRouting的方法:
System.Web.Routing預設提供了一個IRouteHandler-StopRoutingHandler Class,經過它處理的URL不會被做任何處理
通常使用方法如下:
routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
RouteHandler工廠:
其實IRouteHandler可以實現一個RouteHandler的簡單工廠。
複製代碼 代碼如下:

public class RouteHandlerFactory : IRouteHandler
{
string Name { get; set; }
public RouteHandlerFactory(string name){this.Name = name;}
#region IRouteHandler 成員
public IHttpHandler GetHttpHandler(RequestContext requestContext) {
if (this.Name == "mypage")
return new MyPage(requestContext);
if(this.Name="mypage1")
return new MyPage1(requestContext);
}
#endregion
}

規定HTTP verbs,這裡要使用System.Web.Routing中的HttpMethodConstraint
複製代碼 代碼如下:

void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes){
string[] allowedMethods = { "GET", "POST" };
HttpMethodConstraint methodConstraints = new HttpMethodConstraint(allowedMethods);
Route reportRoute = new Route("{locale}/{year}", new ReportRouteHandler());
reportRoute.Constraints = new RouteValueDictionary { { "httpMethod", methodConstraints } };
routes.Add(reportRoute);
}

Demo程式碼下載:
WebApplication3.rar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.