System.Web.Routing入门及进阶 下篇
上面介绍的是最简单的一种定义方式。当然我们可以建立更复杂的规则。其中就包括设定规则的默认值以及设定规则的正则表达式。
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 = ""
}),
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程序代码下载:
http://files.cnblogs.com/chsword/WebApplication3.rar
System.Web.Routing入门及进阶 下篇的更多相关文章
- System.Web.Routing入门及进阶 上篇
System.Web.Routing已经作为一个程序集包含在.net3.5sp1中发布了.虽然我们并没有在3.5sp1中发现Asp.net Mvc的踪迹,但是亦以感觉到它离我们不远了. System. ...
- ASP.net 的URL路由选择(System.Web.Routing.dll)
System.Web.Routing是.net 3.5sp1中新增的一个dll,用它提拱的类可以很方便的实现url的映射,在asp.net WebFrom的编程中可以使客户端请求的URL变得更加的&q ...
- 返璞归真 asp.net mvc (2) - 路由(System.Web.Routing)
原文:返璞归真 asp.net mvc (2) - 路由(System.Web.Routing) [索引页] [源码下载] 返璞归真 asp.net mvc (2) - 路由(System.Web.R ...
- 【ASP.NET】System.Web.Routing - RouteCollection Class
Provides a collection of routes for ASP.NET routing. The RouteCollection class provides methods that ...
- 【ASP.NET】System.Web.Routing - PageRouteHandler Class
用于提供一些属性和方法来定义如何将URL匹配到一个物理文件上面. public PageRouteHandler (string virtualPath, bool checkPhysicalUrlA ...
- 【ASP.NET】System.Web.Routing - HttpMethodConstraint Class
你可以自己定义你的ASP.NET程序接收的get post put 或者delete请求. 使用这个约束的方式为: void Application_Start(object sender, Even ...
- 【ASP.NET】System.Web.Routing - StopRoutingHandler Class
Provides a way to specify that ASP.NET routing should not handle requests for a URL pattern. ex: rou ...
- 【ASP.NET】System.Web.Routing - Route Class
Provides properties and methods for defining a route and for obtaining information about the route. ...
- WEB编程 入门简单 进阶难
其实不论是WEB还是其他什么编程,都是这个道理,至于为什么,我贴几段代码,大家感受下. JS 计算今天是星期几 入门级 // 计算系统当前是星期几 var str =""; var ...
随机推荐
- ESXi 系统管理员命令 [转帖]
来源 https://blog.csdn.net/zy_strive_2012/article/details/53336000 正如所有的虚拟化管理员都知道,要应对VMware基础设施上需要的更多虚 ...
- browser-sync & http server
browser-sync & http server browser-sync https://www.browsersync.io/ usage # step 1 $ npm install ...
- The Two Routes CodeForces - 601A(水最短路)
一个完全图 1和n肯定有一条路 不是公路就是铁路 另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...
- MT【179】最大最小老问题
求$\max\{x^2+2y+20,y^2-6x+12\}$的最小值______ 提示:$4t\ge 3(x^2+2y+20)+y^2-6x+12=3(x-1)^2+(y+3)^2+60\ge 60, ...
- 【USACO 1.4】Combination Lock
/* TASK:combo LANG:C++ URL:http://train.usaco.org/usacoprob2?a=E6RZnAhV9zn&S=combo SOLVE:自己做,想的是 ...
- BZOJ 4361 isn | DP 树状数组
链接 BZOJ 4361 题面 给出一个长度为n的序列A(A1,A2...AN).如果序列A不是非降的,你必须从中删去一个数, 这一操作,直到A非降为止.求有多少种不同的操作方案,答案模10^9+7. ...
- 自动更新前加密:Clickonce用法
一.加密dll 新建一个windows form application: static void Main(string[] args) { Process. ...
- 【转】单片机系统中数字滤波的算法【C程序整理】
随机误差是有随机干搅引起的,其特点是在相同条件下测量同一个量时,其大小和符号做无规则变化而无法预测,但多次测量结果符合统计规律.为克服随机干搅引入的误差,硬件上可采用滤波技术,软件上可以采用软件算法实 ...
- 【POJ2631】Roads in the North 树的直径
题目大意:给定一棵 N 个节点的边权无根树,求树的直径. 代码如下 #include <cstdio> #include <algorithm> using namespace ...
- LInux上返回到切换目录前的目录
在Linux系统上,切换目录用命令cd (change directory),查看当前目录的命令pwd (print working directory). 例如当前目录为:/home/keysyst ...