分享
 
 
 

WebForms使用System.Web.Routing

王朝学院·作者佚名  2009-12-24
窄屏简体版  字體: |||超大  

老赵同学写过 在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:

}相关文章:

<span id="ctl00_ArticleTopHeader_ArticleTitle" class="ArticleTopTitle">Fight 404 errors with ASP.NET Routing :<a href="http://www.codeproject.com/KB/aspnet/routing404.aspx">http://www.codeproject.com/KB/aspnet/routing404.aspx</a></span>

msdn杂志文章:

使用 ASP.NET Web 窗体路由:http://msdn.microsoft.com/zh-cn/magazine/2009.01.extremeaspnet.aspx

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/dz45693/archive/2009/12/22/5058018.aspx

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有