欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

System.Web.Routing入门及进阶

程序员文章站 2024-03-07 15:36:51
urlrouting高级应用 预计效果: 复制代码 代码如下: 当我访问/a/b.aspx时就会转到default.aspx?category=a&action=b在页面上...
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使用正则表达式规则
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则会显示未找到资源。
当然这是里可以使用更高级的正则表达式。
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