龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > asp.net编程 >

Global.asax通过Application_BeginRequest()事件实现访问链接的静态地址映射

时间:2009-12-21 11:47来源:未知 作者:admin 点击:
分享到:
protected void Application_BeginRequest(Objectsender,EventArgse) { string strCurrentPath; strCurrentPath = Request.Path; // makesurethatitendswithaspx.Thisissomequickvalidationonthepath. // Thiswillshortcutprocessingforimagesandotherfiles(W

protected void Application_BeginRequest(Object sender, EventArgs e) {
        
        
string strCurrentPath;
        strCurrentPath 
= Request.Path;
        
//make sure that it ends with aspx.  This is some quick validation on the path. 
        
//This will shortcut processing for images and other files (WebResource.axd, for example). 
        if(System.IO.Path.GetExtension(strCurrentPath).EndsWith(
            
"aspx"true, System.Globalization.CultureInfo.InvariantCulture)) {
            
string strCustomPath;
            
string qString = Request.QueryString.ToString();
            
if(qString != string.Empty)
                qString 
= "&" + qString;
            strCurrentPath 
= strCurrentPath.ToLowerInvariant();
            
string appRoot = GetAppRoot(strCurrentPath);
            
//string pageName = "";
            
// the URL contains this folder name
            
//Response.Write(strCurrentPath);
            if(strCurrentPath.IndexOf("catalog/", StringComparison.InvariantCultureIgnoreCase) > -1{

                appRoot 
= appRoot.Replace("catalog/""");
                strCustomPath 
= appRoot + "catalog.aspx?guid=" + System.IO.Path.GetFileNameWithoutExtension(strCurrentPath) + qString;

                
// rewrite the URL
                Context.RewritePath(strCustomPath, false);
            }

            
else if(strCurrentPath.IndexOf("product/", StringComparison.InvariantCultureIgnoreCase) > -1{
                appRoot 
= appRoot.Replace("product/""");

                strCustomPath 
= appRoot + "product.aspx?guid=" + System.IO.Path.GetFileNameWithoutExtension(strCurrentPath);
                
// rewrite the URL

                Context.RewritePath(strCustomPath, 
false);
            }

        }

    }

精彩图集

赞助商链接