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 .ly 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 . 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: }





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/124188,如需转载请自行联系原作者
目录
相关文章
|
26天前
|
数据库 开发者 Python
web应用开发
【9月更文挑战第1天】web应用开发
37 1
|
14天前
|
数据可视化 图形学 UED
只需四步,轻松开发三维模型Web应用
为了让用户更方便地应用三维模型,阿里云DataV提供了一套完整的三维模型Web模型开发方案,包括三维模型托管、应用开发、交互开发、应用分发等完整功能。只需69.3元/年,就能体验三维模型Web应用开发功能!
36 8
只需四步,轻松开发三维模型Web应用
|
4天前
|
安全 API 开发者
Web 开发新风尚!Python RESTful API 设计与实现,让你的接口更懂开发者心!
在当前的Web开发中,Python因能构建高效简洁的RESTful API而备受青睐,大大提升了开发效率和用户体验。本文将介绍RESTful API的基本原则及其在Python中的实现方法。以Flask为例,演示了如何通过不同的HTTP方法(如GET、POST、PUT、DELETE)来创建、读取、更新和删除用户信息。此示例还包括了基本的路由设置及操作,为开发者提供了清晰的API交互指南。
27 6
|
3天前
|
存储 JSON API
实战派教程!Python Web开发中RESTful API的设计哲学与实现技巧,一网打尽!
在数字化时代,Web API成为连接前后端及构建复杂应用的关键。RESTful API因简洁直观而广受欢迎。本文通过实战案例,介绍Python Web开发中的RESTful API设计哲学与技巧,包括使用Flask框架构建一个图书管理系统的API,涵盖资源定义、请求响应设计及实现示例。通过准确使用HTTP状态码、版本控制、错误处理及文档化等技巧,帮助你深入理解RESTful API的设计与实现。希望本文能助力你的API设计之旅。
17 3
|
4天前
|
JSON API 数据库
从零到英雄?一篇文章带你搞定Python Web开发中的RESTful API实现!
在Python的Web开发领域中,RESTful API是核心技能之一。本教程将从零开始,通过实战案例教你如何使用Flask框架搭建RESTful API。首先确保已安装Python和Flask,接着通过创建一个简单的用户管理系统,逐步实现用户信息的增删改查(CRUD)操作。我们将定义路由并处理HTTP请求,最终构建出功能完整的Web服务。无论是初学者还是有经验的开发者,都能从中受益,迈出成为Web开发高手的重要一步。
24 4
|
2天前
|
开发框架 JSON 缓存
震撼发布!Python Web开发框架下的RESTful API设计全攻略,让数据交互更自由!
在数字化浪潮推动下,RESTful API成为Web开发中不可或缺的部分。本文详细介绍了在Python环境下如何设计并实现高效、可扩展的RESTful API,涵盖框架选择、资源定义、HTTP方法应用及响应格式设计等内容,并提供了基于Flask的示例代码。此外,还讨论了版本控制、文档化、安全性和性能优化等最佳实践,帮助开发者实现更流畅的数据交互体验。
13 1
|
4天前
|
JSON API 开发者
惊!Python Web开发新纪元,RESTful API设计竟能如此性感撩人?
在这个Python Web开发的新纪元里,RESTful API的设计已经超越了简单的技术实现,成为了一种追求极致用户体验和开发者友好的艺术表达。通过优雅的URL设计、合理的HTTP状态码使用、清晰的错误处理、灵活的版本控制以及严格的安全性措施,我们能够让RESTful API变得更加“性感撩人”,为Web应用注入新的活力与魅力。
15 3
|
4天前
|
SQL 安全 Go
SQL注入不可怕,XSS也不难防!Python Web安全进阶教程,让你安心做开发!
在Web开发中,安全至关重要,尤其要警惕SQL注入和XSS攻击。SQL注入通过在数据库查询中插入恶意代码来窃取或篡改数据,而XSS攻击则通过注入恶意脚本来窃取用户敏感信息。本文将带你深入了解这两种威胁,并提供Python实战技巧,包括使用参数化查询和ORM框架防御SQL注入,以及利用模板引擎自动转义和内容安全策略(CSP)防范XSS攻击。通过掌握这些方法,你将能够更加自信地应对Web安全挑战,确保应用程序的安全性。
23 3
|
6天前
|
JSON API 数据格式
深度剖析!Python Web 开发中 RESTful API 的每一个细节,你不可不知的秘密!
在 Python Web 开发中,RESTful API 是构建强大应用的关键,基于 Representational State Transfer 架构风格,利用 HTTP 卞性能。通过 GET、POST、PUT 和 DELETE 方法分别实现资源的读取、创建、更新和删除操作。示例代码展示了如何使用 Flask 路由处理这些请求,并强调了状态码的正确使用,如 200 表示成功,404 表示未找到资源等。
26 5
|
23天前
|
数据采集 Java 数据挖掘
Java IO异常处理:在Web爬虫开发中的实践
Java IO异常处理:在Web爬虫开发中的实践