网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> .NET专栏 >> 浏览文章
 
 
WebForms使用System.Web.Routing
作者:佚名 来源:帝网科技 日期:2009年12月28日 点击数:


  在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:

  ///

  20:

  /// This is the full virtual path (using tilde syntax) to the WebForm page.

  21:

  ///

  22:

  ///

  23:

  /// Needs to be thread safe so this is only settable via ctor.

  24:

  ///

  25:

  public

  string

  VirtualPath { get; private

  set; } 26:

  27:

  ///

  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:

  ///

  33:

  /// True by default

  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:

  ///

  53:

  /// Gets the virtual path to the resource after applying substitutions based on route data.

  54:

  ///

  55:

  ///

  56:

  ///

  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:

  }相关文章:

  Fight 404 errors with ASP.NET Routing :http://www.codeproject.com/KB/aspnet/routing404.aspx

  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

  相关文章
 
·ASP.NET使用log4Net日志组件教程(日志
·ASP.NET MVC 框架
·C#实现的BinaryTree
·WebForms使用System.Web.Routing
·ASP.NET获取远程网页下载到本地文件
·一个“简单”的ASP.NET的服务器控件
·ASP.net与PHP两大网站开发架构优势对比
·教你七招提高.NET网站性能
·ASP.NET未来:简化开发 HTML5性能提升
·ASP.NET实现类似Excel的数据透视表
·FileUpload上传多文件出现错误的解决方
·.NET从优酷专辑中采集所有视频及信息(
·ASP.NET 4中的SEO改进
·详解Asp.net MVC DropDownLists
·提高ASP.NET应用程序性能的几招方法
·asp.net实现51job地区选择效果
·ASP.NET中创建GeoRSS订阅源
·ASP.NET 4.0开发更加简便
·ASP.NET页面间数据传递的方法
·ASP.NET的SEO:使用.ashx文件——排除
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号