下面的文章只是我自己的一种思路,不对的地方还请多指教
现在的BLOG注册后的帐号都是在网站域名后加一个目录,比如用帐号"dudu"注册后,直接访问www.cnblogs.com/dudu就可以了。但我猜想服务器不会为每个帐号创建一个目录,那如何实现的呢,虚URL是一种解决方法。其实说起来很简单,就是在Global.aspx的BeginRequest事件中捕捉用户请求的路径,如果有特定用户信息,则以这个用户为参数,重定向到服务器上特定的页面。比如:
  

虚URL地址的实现思路<Script Language="C#" Runat="Server">
虚URL地址的实现思路
{
虚URL地址的实现思路    
string strPageOwner;
虚URL地址的实现思路    
string strCurrentPath;
虚URL地址的实现思路    
string strUserPath;
虚URL地址的实现思路    
string strUserName;
虚URL地址的实现思路    
虚URL地址的实现思路    strCurrentPath 
= Request.Path.ToLower();
虚URL地址的实现思路    
//检查请求的路径是否为网站的实际页面,只有请求虚Url才重定向
虚URL地址的实现思路
    if (strCurrentPath.IndexOf( "/site/" ) == -1
虚URL地址的实现思路        
&& strCurrentPath.IndexOf( "userpage.aspx" ) == -1 
虚URL地址的实现思路        
&& strCurrentPath.IndexOf( "default.aspx" ) == -1
{
虚URL地址的实现思路    strUserName 
= GetUserPathByUrl(Request.Path);//根据请求的路径得出用户信息
虚URL地址的实现思路
    strUserPath = String.Format("~/userpage.aspx?username={0}",strUserName );
虚URL地址的实现思路    Context.RewritePath( strUserPath );
虚URL地址的实现思路    }

虚URL地址的实现思路}

虚URL地址的实现思路
//根据请求路径得出用户信息,如:/pentium/archive/33431.aspx,得出用户pentium
虚URL地址的实现思路
string GetPathByUrl(string strUrl)
{
虚URL地址的实现思路    strUrl
=strUrl.Replace("/","\\");
虚URL地址的实现思路    
while (strUrl.LastIndexOf("\\")!=0)
{
虚URL地址的实现思路        strUrl
=System.IO.Path.GetDirectoryName(strUrl);
虚URL地址的实现思路    }

虚URL地址的实现思路    
if(strUrl.StartsWith("\\"))
虚URL地址的实现思路        strUrl
=strUrl.Remove(0,1);            
虚URL地址的实现思路    
return strUrl;
虚URL地址的实现思路}

虚URL地址的实现思路
</Script>


更复杂的逻辑可以用httpModule实现,这仅仅是一种思路:截获用户请求的地址,得出所请求的信息,然后重定向页面

相关文章:

  • 2021-08-29
  • 2021-12-06
  • 2021-06-02
  • 2022-01-06
  • 2021-07-20
  • 2021-11-18
  • 2021-06-11
猜你喜欢
  • 2021-12-12
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
  • 2021-05-20
相关资源
相似解决方案