System.Web.Routing入門及進(jìn)階
更新時(shí)間:2011年12月22日 22:04:18 作者:
上面介紹的是最簡(jiǎn)單的一種定義方式。當(dāng)然我們可以建立更復(fù)雜的規(guī)則。其中就包括設(shè)定規(guī)則的默認(rèn)值以及設(shè)定規(guī)則的正則表達(dá)式
UrlRouting高級(jí)應(yīng)用
預(yù)計(jì)效果:
當(dāng)我訪問/a/b.aspx時(shí)就會(huì)轉(zhuǎn)到Default.aspx?category=a&action=b在頁面上顯示
category:a
action:b
亦如果我訪問/chsword/xxxx.aspx就會(huì)轉(zhuǎn)到Default.aspx?category=chsword&action=xxxx就會(huì)顯示
category:chsword
action:xxxx
如果訪問/chsword/就會(huì)轉(zhuǎn)到 Default.aspx?category=chsword&action=index就會(huì)顯示
category:chsword
action:index
首先我建立一個(gè)Route
routes.Add(
"Default",
new Route("{category}/{action}.aspx",
new RouteValueDictionary(
new
{
file = "Default",
category = "home",
action = "index"
}), new MyRouteHandler()
)
);
當(dāng)然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"] %>
以顯示傳入的參數(shù)。
如果在IIS中設(shè)置Index.aspx時(shí)就算輸入/a/也會(huì)訪問到/a/index.aspx,即默認(rèn)的會(huì)按RouteValueDictionary中設(shè)置的值自動(dòng)補(bǔ)全
UrlRouting使用正則表達(dá)式規(guī)則
UrlRouting在定義的時(shí)候也可以按正則的規(guī)則來進(jìn)行定義。
routes.Add(
"zz",
new Route("{category}/{action}.chs",
new RouteValueDictionary(
new {
file = "Default",
category = "home",
action = "1"
}),
new RouteValueDictionary(
new {
action = "[\\d]+"
}),
new MyRouteHandler()
)
);
以上代碼規(guī)定了action只能是數(shù)字則訪問/a/1.chs可以正常訪問。
而訪問/a/b.chs則會(huì)顯示未找到資源。
當(dāng)然這是里可以使用更高級(jí)的正則表達(dá)式。
UrlRouting的技巧
排除UrlRouting的方法:
System.Web.Routing默認(rèn)提供了一個(gè)IRouteHandler-StopRoutingHandler Class,經(jīng)過它處理的URL不會(huì)被做任何處理
通常使用方法如下:
routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
RouteHandler工廠:
其實(shí)IRouteHandler可以實(shí)現(xiàn)一個(gè)RouteHandler的簡(jiǎn)單工廠。
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
}
規(guī)定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
預(yù)計(jì)效果:
復(fù)制代碼 代碼如下:
當(dāng)我訪問/a/b.aspx時(shí)就會(huì)轉(zhuǎn)到Default.aspx?category=a&action=b在頁面上顯示
category:a
action:b
亦如果我訪問/chsword/xxxx.aspx就會(huì)轉(zhuǎn)到Default.aspx?category=chsword&action=xxxx就會(huì)顯示
category:chsword
action:xxxx
如果訪問/chsword/就會(huì)轉(zhuǎn)到 Default.aspx?category=chsword&action=index就會(huì)顯示
category:chsword
action:index
首先我建立一個(gè)Route
復(fù)制代碼 代碼如下:
routes.Add(
"Default",
new Route("{category}/{action}.aspx",
new RouteValueDictionary(
new
{
file = "Default",
category = "home",
action = "index"
}), new MyRouteHandler()
)
);
當(dāng)然IHttpHandler的處理方式也要有所改變
為了方便查看我使用了下方法:
復(fù)制代碼 代碼如下:
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中寫如下代碼:
復(fù)制代碼 代碼如下:
category:<%=Request.Params["category"] %><br />
action:<%=Request.Params["action"] %>
以顯示傳入的參數(shù)。
如果在IIS中設(shè)置Index.aspx時(shí)就算輸入/a/也會(huì)訪問到/a/index.aspx,即默認(rèn)的會(huì)按RouteValueDictionary中設(shè)置的值自動(dòng)補(bǔ)全
UrlRouting使用正則表達(dá)式規(guī)則
UrlRouting在定義的時(shí)候也可以按正則的規(guī)則來進(jìn)行定義。
復(fù)制代碼 代碼如下:
routes.Add(
"zz",
new Route("{category}/{action}.chs",
new RouteValueDictionary(
new {
file = "Default",
category = "home",
action = "1"
}),
new RouteValueDictionary(
new {
action = "[\\d]+"
}),
new MyRouteHandler()
)
);
以上代碼規(guī)定了action只能是數(shù)字則訪問/a/1.chs可以正常訪問。
而訪問/a/b.chs則會(huì)顯示未找到資源。
當(dāng)然這是里可以使用更高級(jí)的正則表達(dá)式。
UrlRouting的技巧
排除UrlRouting的方法:
System.Web.Routing默認(rèn)提供了一個(gè)IRouteHandler-StopRoutingHandler Class,經(jīng)過它處理的URL不會(huì)被做任何處理
通常使用方法如下:
routes.Add(new Route("{resource}.axd/{*pathInfo}", new StopRoutingHandler()));
RouteHandler工廠:
其實(shí)IRouteHandler可以實(shí)現(xiàn)一個(gè)RouteHandler的簡(jiǎn)單工廠。
復(fù)制代碼 代碼如下:
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
}
規(guī)定HTTP verbs,這里要使用System.Web.Routing中的HttpMethodConstraint
復(fù)制代碼 代碼如下:
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
您可能感興趣的文章:
- ASP.NET組件System.Web.Optimization原理及緩存問題詳解
- 無法將類型為“System.Web.UI.WebControls.HiddenField”的對(duì)象強(qiáng)制轉(zhuǎn)換為類型的錯(cuò)誤的解決
- System.Web.Routing入門及進(jìn)階
- NET Runtime Optimization Service 1101 錯(cuò)誤的解決方法
- ASP.NET MVC命名空間時(shí)引起錯(cuò)誤的解決方法
- ASP.Net中命名空間Namespace淺析和使用例子
- System.Web中不存在類型或命名空間名稱“Optimization”(是否缺少程序集引用?)
相關(guān)文章
ASP.NET中等安全模式的一些經(jīng)驗(yàn)分享
如果你正在開發(fā)一個(gè)通用型的Web產(chǎn)品,比如BBS、CMS、BLOG這類的,那么,建議你閱讀以下本篇文章。2010-01-01
ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法總結(jié)
這篇文章主要介紹了ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法,結(jié)合實(shí)例分析總結(jié)了ASP.NET連接數(shù)據(jù)庫及獲取數(shù)據(jù)的相關(guān)實(shí)現(xiàn)技巧,并附帶了web.config配置文件的使用方法與相關(guān)注意事項(xiàng),需要的朋友可以參考下2015-11-11
利用Aspose.Cells實(shí)現(xiàn)萬能導(dǎo)出功能
這篇文章主要為大家詳細(xì)介紹了利用Aspose.Cells實(shí)現(xiàn)萬能導(dǎo)出功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12
涉及網(wǎng)絡(luò)編程時(shí),需要用到的幾個(gè)常用方法
涉及網(wǎng)絡(luò)編程時(shí),需要用到的幾個(gè)常用方法...2006-09-09
Ajax異步無刷新對(duì)局部數(shù)據(jù)更新
Ajax異步無刷新對(duì)局部數(shù)據(jù)更新的實(shí)例2013-03-03

