WebForms使用System.Web.Routing
时间:2022-04-23
本文章向大家介绍WebForms使用System.Web.Routing,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
老赵同学写过 在Web应用程序开发过程中利用ASP.NET MVC框架的实战技巧 ,Routing现在可以作为URLRewriting技术的替代者,出现在asp.net mvc框架中,将它应用于WebForms上也是很简单的,可以到codeplex上下载ASP.NET MVC WebFormRouting Demo。
实现的原理也是很简单的:
1、创建一个自定义的实例化你的页面的 IRouteHandler
1: public class WebFormRouteHandler : IRouteHandler {
2: public WebFormRouteHandler(string virtualPath)
3: : this(virtualPath, true) {
4: }
5:
6: public WebFormRouteHandler(string virtualPath, bool checkPhysicalUrlAccess) {
7: if (virtualPath == null) {
8: throw new ArgumentNullException("virtualPath");
9: }
10:
11: if (!virtualPath.StartsWith("~/")) {
12: throw new ArgumentException("virtualPath must start with a tilde slash: "~/"", "virtualPath");
13: }
14:
15: this.VirtualPath = virtualPath;
16: this.CheckPhysicalUrlAccess = checkPhysicalUrlAccess;
17: }
18:
19: /// <summary>
20: /// This is the full virtual path (using tilde syntax) to the WebForm page.
21: /// </summary>
22: /// <remarks>
23: /// Needs to be thread safe so this is only settable via ctor.
24: /// </remarks>
25: public string VirtualPath { get; private set; }
26:
27: /// <summary>
28: /// Because we're not actually rewriting the URL, ASP.NET's URL Auth will apply
29: /// to the incoming request URL and not the URL of the physical WebForm page.
30: /// Setting this to true (default) will apply URL access rules against the
31: /// physical file.
32: /// </summary>
33: /// <value>True by default</value>
34: public bool CheckPhysicalUrlAccess { get; set; }
35:
36: public IHttpHandler GetHttpHandler(RequestContext requestContext) {
37: string virtualPath = GetSubstitutedVirtualPath(requestContext);
38: if (this.CheckPhysicalUrlAccess && !UrlAuthorizationModule.CheckUrlAccessForPrincipal(virtualPath, requestContext.HttpContext.User, requestContext.HttpContext.Request.HttpMethod))
39: throw new SecurityException();
40:
41: var page = BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as IHttpHandler;
42: if (page != null) {
43: //Pages that don't implement IRoutablePage won't have the RequestContext
44: //available to them. Can't generate outgoing routing URLs without that context.
45: var routablePage = page as IRoutablePage;
46: if (routablePage != null)
47: routablePage.RequestContext = requestContext;
48: }
49: return page;
50: }
51:
52: /// <summary>
53: /// Gets the virtual path to the resource after applying substitutions based on route data.
54: /// </summary>
55: /// <param name="requestContext"></param>
56: /// <returns></returns>
57: public string GetSubstitutedVirtualPath(RequestContext requestContext) {
58: if (!VirtualPath.Contains("{"))
59: return VirtualPath;
60:
61: //Trim off ~/
62: string virtualPath = VirtualPath.Substring(2);
63:
64: Route route = new Route(virtualPath, this);
65: VirtualPathData vpd = route.GetVirtualPath(requestContext, requestContext.RouteData.Values);
66: if (vpd == null)
67: return VirtualPath;
68: return "~/" + vpd.VirtualPath;
69: }
70: }
2、使用自定义的 IRouteHandler注册一个新的Routes
1: public class Global : System.Web.HttpApplication
2: {
3:
4: protected void Application_Start(object sender, EventArgs e)
5: {
6: RegisterRoutes(RouteTable.Routes);
7: }
8:
9: public static void RegisterRoutes(RouteCollection routes)
10: {
11: //We are intentionally creating this backdoor as a demonstration of
12: //bad security practices.
13: routes.MapWebFormRoute("Secret", "BackDoor", "~/Admin/SecretPage.aspx", false);
14: routes.MapWebFormRoute("Blocked", "FrontDoor", "~/Admin/SecretPage.aspx", true);
15:
16: //Even though we are not checking physical url access in this route, it should still block because the incoming
17: //request url would start with /Admin.
18: routes.MapWebFormRoute("Admin", "Admin/{*anything}", "~/Admin/SecretPage.aspx", false);
19:
20: routes.MapWebFormRoute("Named", "foo/bar", "~/forms/blech.aspx");
21: routes.MapWebFormRoute("Numbers", "one/two/three", "~/forms/haha.aspx");
22:
23: //Maps any requests for /haha/*.aspx to /forms/hahah.aspx
24: routes.MapWebFormRoute("Substitution", "haha/{filename}", "~/forms/haha.aspx");
25: }
26: }
msdn杂志文章: 使用 ASP.NET Web 窗体路由:http://msdn.microsoft.com/zh-cn/magazine/2009.01.extremeaspnet.aspx
- Linux服务器数据定期同步和备份方式
- Android组件化搭建
- ECMAScript 6 入门简介
- RxJava2 实战知识梳理(2) - 计算一段时间内数据的平均值
- 用了Docker,妈妈再也不担心我的软件安装了 - 基础篇
- 适配器模式(Adapter)
- 敞开的地狱之门:Kerberos协议的滥用
- CY7C68013A的一点总结
- RxJava2 实战(1) - 后台执行耗时操作,实时通知 UI 更新
- AutoIt木马又一发:暗藏神秘照片
- css3动画从入门到精通
- ReactJs和React Native的那些事
- Linux学习-文件排序和FASTA文件操作
- Bash漏洞再次演进:缓冲区溢出导致远程任意命令执行
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- shell脚本快速入门之-----数组
- JSTL
- shell脚本快速入门之-----正则三剑客之三awk用法大全!!!
- 一起来看看1stOpt到底有多强大?
- shell脚本快速入门之-----深入介绍Expect免交互
- shell脚本快速入门之-----Here document使用方法总结
- spring框架应用系列一:annotation-config自动装配
- 弄它!!!2020年了你还不懂ACL嘛,小编带你走进ACL的世界,分分钟搞定!!!
- jsp实用过滤器写法
- 搞它!!! Linux 查看CPU信息,机器型号,内存等信息
- shell脚本快速入门之----sort、uniq和tr
- 社区开源框架网络模块:ConnectionManager详解
- 搞它!!!Linux--深入介绍firewalld防火墙管理工具
- 搞它!!! 深入介绍iptables防火墙管理工具
- ZooKeeper的十二连问,你顶得了嘛?