利用asp.net 2.0构建企业级门户平台(2、实现页面请求的调度)

博客介绍了页面调度采用的UrlRewrite技术,可参考相关文章。还说明了使用VWD 2005 Express BETA2新建网站的步骤,添加default.aspx让IIS转发请求,在Global.asax截获用户请求,也可将代码放于Global.asax.cs或实现IHttpModuler达到类似效果。

页面调度采用了UrlRewrite技术,关于该技术的资料可以参考我转发的一篇文章:http://erpcrm.cnblogs.com/articles/232924.html

首先,让我们新建一个网站(我使用的是 VWD 2005 Express BETA2)。
1、添加一个default.aspx ,
该窗体不需要做什么工作,它的存在只有一个意义,就是告诉IIS 把类似的请求(www.xxx.com/)转过来,否则的话,ASP.NET是截获不到这种请求的。
2、我们在哪截获用户的请求呢?
当然是Global.asax了(当然了,你可以把代码放到Global.asax.cs中,或者自己实现IHttpModuler来达到类似的效果)。
代码如下:

None.gif ;Global.asax文件,只有一行,可以看出具体的代码文件都在Global.asax.cs里面
None.gif
<% @ Application Language = " C# "  CodeBehind = " Global.asax.cs "  Inherits = " Global "   %>

Global.asax.cs文件:
None.gif using  System;
None.gif
using  System.Data;
None.gif
using  System.Configuration;
None.gif
using  System.Web;
None.gif
using  System.Web.Security;
None.gif
using  System.Web.UI;
None.gif
using  System.Web.UI.WebControls;
None.gif
using  System.Web.UI.WebControls.WebParts;
None.gif
using  System.Web.UI.HtmlControls;
None.gif
None.gif
None.gif
public   class  Global : System.Web.HttpApplication
ExpandedBlockStart.gif
{
InBlock.gif        
protected void Application_BeginRequest(Object sender, EventArgs e)
ExpandedSubBlockStart.gif     
{
InBlock.gif       
InBlock.gif              String appPath 
= Request.AppRelativeCurrentExecutionFilePath;
InBlock.gif
InBlock.gif              
if (appPath.StartsWith("~/Admin/"truenull))
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (appPath.Equals("~/WebResource.axd", StringComparison.OrdinalIgnoreCase))
ExpandedSubBlockStart.gif        
{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
if (appPath.StartsWith("~/ApplicationTemplate/", StringComparison.OrdinalIgnoreCase))
ExpandedSubBlockStart.gif      
{
InBlock.gif            
return;
ExpandedSubBlockEnd.gif        }

InBlock.gif               
InBlock.gif            
this.Context.RewritePath("~/ApplicationTemplate/DefaultTemplate.aspx");                 
InBlock.gif
ExpandedSubBlockEnd.gif      }
  
InBlock.gif
ExpandedBlockEnd.gif }

None.gif
从以上的代码可以看出:
除那三个特殊的路径外,其它的请求全部重写到 " ~/ApplicationTemplate/DefaultTemplate.aspx ",
那么
 DefaultTemplate.aspx 包含什么呢?      
让我们新建一个文件夹 ApplicationTemplate 在该文件夹下新建一个web窗体 DefaultTemplate.aspx。
只 是为了演示UrlRewrite,DefaultTemplate.aspx中你可以输入一些简单的内容,如:“这是一个默认的模版”;
这样当我们请求“/default.aspx”时,呈现在我们面前的页面是DefaultTemplate.aspx的内容。
不信?你先动手试试吧!

3、如果我们想 把 / 重写到 ApplicatonTemplate/defaultTemplate.aspx,而把 /product/ 重写到ApplicationTemplate/ProductTemplate.aspx,怎么做呢?

首先我们在ApplicationTemplate文件夹下,添加一个ProductTemplate.aspx。
我们可能会想到在 Global.asax.cs 的 Application_BeginReque方法里再添加一个对路径的判断,显然这是不灵活的。
怎么更灵活呢?就让我们来设计一个页面调度引擎吧!
在这里,我们叫他 ApplicationManager。
添加一个类文件 ApplicationManager.cs ,   VWD提示我们要把它放到 App_Code目录下,就按它说的办吧!

该类有个方法叫  String  GetNewPath(String  oldPath);
我们要这个方法输入“/ default.aspx”返回 “~/ApplicationTemplate/DefaultTemplate.aspx”,
输入"/product/" 返回 “~/ApplicationTemplate/ProductTemplate.aspx” 
如果输入的是“/Admin”,还应该返回“/Admin”。

我们假定GetNewPath()方法有这个功能,那么让我们改造一下 :Applicaton_BeginReques;

None.gif   protected   void  Application_BeginRequest(Object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif       
InBlock.gif        String appPath 
= Request.AppRelativeCurrentExecutionFilePath;
InBlock.gif   String newPath 
= ApplicationManager.GetNewPath(appPath) 
InBlock.gif        
if(newPath != appPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif
dot.gif{
InBlock.gif 
this.Context.RewritePath(newPath);
ExpandedSubBlockEnd.gif}
              
InBlock.gif
ExpandedBlockEnd.gif    }

今天先到这,我的下一篇文章会把源码提供出来,也许会和本文中的不太一致,但意思不会差。


转载于:https://www.cnblogs.com/erpcrm/archive/2005/09/09/233181.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值