原文来自http://blog.joycode.com/ceocio/archive/2005/02/25/44862.aspx
ASPNET Forums中是将所有的站点链接都保存到一个XML文件中,通过程序对XML文件进行访问,或者直接取出链接,或者将链接加上参数。
这个文件的格式是这样的:
![]() xml version="1.0" encoding="utf-8" ?>
xml version="1.0" encoding="utf-8" ?> 
![]() <urls>
<urls>
![]() <url name="errorMessage" path="/error.aspx?ErrorMessageID={0}" />
    <url name="errorMessage" path="/error.aspx?ErrorMessageID={0}" />
![]() <url name="News" path="/News.aspx?TID={0}^CId={1}" />
    <url name="News" path="/News.aspx?TID={0}^CId={1}" />
![]() urls>
urls>
这里用^替代了&符号。
我们只需要读出这个XML文件就可以了,也可以把读出的链接信息缓存起来,然后再调用。
SiteUrls.cs
![]() using System;
using System;
![]() using System.Web;
using System.Web;
![]() using System.Collections;
using System.Collections;
![]() using System.Collections.Specialized;
using System.Collections.Specialized;
![]() using System.Xml;
using System.Xml;
![]() using WestRoad.Trip.FrameWork.Enumerations;
using WestRoad.Trip.FrameWork.Enumerations;
![]() 
![]() namespace WestRoad.Trip.FrameWork
namespace WestRoad.Trip.FrameWork
![]()
![]() ...{
...{
![]()
![]() /**//// <summary>
    /**//// <summary>
![]() /// SiteUrls 的摘要说明。
    /// SiteUrls 的摘要说明。
![]() /// </summary>
    /// </summary>
![]() public class SiteUrls
    public class SiteUrls 
![]()
![]() ...{
    ...{
![]() 
![]()
![]() Member variables & constructor#region Member variables & constructor
        Member variables & constructor#region Member variables & constructor
![]() SiteContext siteContext = SiteContext.Current;
        SiteContext siteContext = SiteContext.Current;
![]() NameValueCollection paths = null;
        NameValueCollection paths = null;
![]() NameValueCollection reversePaths = null;
        NameValueCollection reversePaths = null;
![]() string siteUrlsXmlFile = "/Resource/SiteUrls.config";
        string siteUrlsXmlFile = "/Resource/SiteUrls.config";
![]() bool enableWaitPage = false;
        bool enableWaitPage = false;
![]() 
![]() // 从siteUrls.config文件读取站点的链接
        // 从siteUrls.config文件读取站点的链接
![]() // 或者从cache中读取站点链接
        // 或者从cache中读取站点链接
![]() public SiteUrls()
        public SiteUrls() 
![]()
![]() ...{
        ...{
![]() string cacheKey = "SiteUrls";
            string cacheKey = "SiteUrls";
![]() string cacheKeyReverse = "SiteUrls-ReverseLookup";
            string cacheKeyReverse = "SiteUrls-ReverseLookup";
![]() 
![]() if (HttpRuntime.Cache[cacheKey] == null)
            if (HttpRuntime.Cache[cacheKey] == null) 
![]()
![]() ...{
            ...{
![]() string file = "";
                string file = "";
![]() 
![]() if (siteContext != null)
                if (siteContext != null)
![]() file = siteContext.Context.Server.MapPath(Globals.ApplicationPath + siteUrlsXmlFile);
                    file = siteContext.Context.Server.MapPath(Globals.ApplicationPath + siteUrlsXmlFile);
![]() else
                else
![]() file = HttpContext.Current.Server.MapPath(Globals.ApplicationPath + siteUrlsXmlFile);
                    file = HttpContext.Current.Server.MapPath(Globals.ApplicationPath + siteUrlsXmlFile);
![]() 
![]() paths = new NameValueCollection();
                paths = new NameValueCollection();
![]() reversePaths = new NameValueCollection();
                reversePaths = new NameValueCollection();
![]() 
![]() // 读取xml文件
                // 读取xml文件
![]() //
                //
![]() XmlDocument doc = new XmlDocument();
                XmlDocument doc = new XmlDocument();
![]() doc.Load( file );
                doc.Load( file );
![]() 
![]() XmlNode urls = doc.SelectSingleNode("urls");
                XmlNode urls = doc.SelectSingleNode("urls");
![]() 
![]() foreach (XmlNode n in urls.ChildNodes)
                foreach (XmlNode n in urls.ChildNodes) 
![]()
![]() ...{
                ...{
![]() 
![]() if (n.NodeType != XmlNodeType.Comment)
                    if (n.NodeType != XmlNodeType.Comment) 
![]()
![]() ...{
                    ...{
![]() string name = n.Attributes["name"].Value;
                        string name = n.Attributes["name"].Value;
![]() string path = ResolveUrl(n.Attributes["path"].Value.Replace("^", "&"));
                        string path = ResolveUrl(n.Attributes["path"].Value.Replace("^", "&"));
![]() paths.Add(name, path);
                        paths.Add(name, path);
![]() reversePaths.Add(n.Attributes["path"].Value.Replace("^", "&"), name);
                        reversePaths.Add(n.Attributes["path"].Value.Replace("^", "&"), name);
![]() }
                    }
![]() 
![]() }
                }
![]() 
![]() System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(file);
                System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(file);
![]() System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(file);
                System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(file);
![]() 
![]() HttpRuntime.Cache.Insert(cacheKey, paths, dep, DateTime.MaxValue, TimeSpan.Zero);
                HttpRuntime.Cache.Insert(cacheKey, paths, dep, DateTime.MaxValue, TimeSpan.Zero);
![]() HttpRuntime.Cache.Insert(cacheKeyReverse, reversePaths, dep2, DateTime.MaxValue, TimeSpan.Zero);
                HttpRuntime.Cache.Insert(cacheKeyReverse, reversePaths, dep2, DateTime.MaxValue, TimeSpan.Zero);
![]() 
![]() }
            }
![]() 
![]() paths = (NameValueCollection) HttpRuntime.Cache[cacheKey];
            paths = (NameValueCollection) HttpRuntime.Cache[cacheKey];
![]() reversePaths = (NameValueCollection) HttpRuntime.Cache[cacheKeyReverse];
            reversePaths = (NameValueCollection) HttpRuntime.Cache[cacheKeyReverse];
![]() 
![]() }
        }
![]() #endregion
        #endregion
![]() 
![]() 
![]() //eg:return a site url
        //eg:return a site url
![]() 
![]() public string News(int TID, int CID)
        public string News(int TID, int CID) 
![]()
![]() ...{
        ...{
![]() return string.Format(paths["News"], TID.ToString(), CID.ToString());
            return string.Format(paths["News"], TID.ToString(), CID.ToString());
![]() }
        }
![]() public string ErrorMessage (SiteExceptionType exceptionType)
        public string ErrorMessage (SiteExceptionType exceptionType) 
![]()
![]() ...{
        ...{
![]() return string.Format( paths["errormessage"], ((int) exceptionType).ToString());
            return string.Format( paths["errormessage"], ((int) exceptionType).ToString());
![]() }
        }
![]() 
![]()
![]() Private static helper methods#region Private static helper methods
        Private static helper methods#region Private static helper methods
![]() private static string ResolveUrl (string path)
        private static string ResolveUrl (string path) 
![]()
![]() ...{
        ...{
![]() 
![]() if (Globals.ApplicationPath.Length > 0)
            if (Globals.ApplicationPath.Length > 0)
![]() return Globals.ApplicationPath + path;
                return Globals.ApplicationPath + path;
![]() else
            else
![]() return path;
                return path;
![]() }
        }
![]() 
![]() public static string RemoveParameters (string url)
        public static string RemoveParameters (string url) 
![]()
![]() ...{
        ...{
![]() 
![]() if (url == null)
            if (url == null)
![]() return string.Empty;
                return string.Empty;
![]() 
![]() int paramStart = url.IndexOf("?");
            int paramStart = url.IndexOf("?");
![]() 
![]() if (paramStart > 0)
            if (paramStart > 0)
![]() return url.Substring(0, paramStart);
                return url.Substring(0, paramStart);
![]() 
![]() return url;
            return url;
![]() }
        }
![]() 
![]() private static NameValueCollection ExtractQueryParams(string url)
        private static NameValueCollection ExtractQueryParams(string url) 
![]()
![]() ...{
        ...{
![]() int startIndex = url.IndexOf("?");
            int startIndex = url.IndexOf("?");
![]() NameValueCollection values = new NameValueCollection();
            NameValueCollection values = new NameValueCollection();
![]() 
![]() if (startIndex <= 0)
            if (startIndex <= 0)
![]() return values;
                return values;
![]() 
![]() string[] nameValues = url.Substring(startIndex + 1).Split('&');
            string[] nameValues = url.Substring(startIndex + 1).Split('&');
![]() 
![]() foreach (string s in nameValues)
            foreach (string s in nameValues) 
![]()
![]() ...{
            ...{
![]() string[] pair = s.Split('=');
                string[] pair = s.Split('=');
![]() 
![]() string name = pair[0];
                string name = pair[0];
![]() string value = string.Empty;
                string value = string.Empty;
![]() 
![]() if (pair.Length > 1)
                if (pair.Length > 1)
![]() value = pair[1];
                    value = pair[1];
![]() 
![]() values.Add(name, value);
                values.Add(name, value);
![]() }
            }
![]() 
![]() return values;
            return values;
![]() }
        }
![]() 
![]() public static string FormatUrlWithParameters(string url, string parameters)
        public static string FormatUrlWithParameters(string url, string parameters) 
![]()
![]() ...{
        ...{
![]() if (url == null)
            if (url == null)
![]() return string.Empty;
                return string.Empty;
![]() 
![]() if (parameters.Length > 0)
            if (parameters.Length > 0)
![]() url = url + "?" + parameters;
                url = url + "?" + parameters;
![]() 
![]() return url;
            return url;
![]() 
![]() }
        }
![]() #endregion
        #endregion
![]() 
![]() 
![]()
![]() Public properties#region Public properties
        Public properties#region Public properties
![]() public bool EnableWaitPage
        public bool EnableWaitPage 
![]()
![]() ...{
        ...{
![]() get
            get 
![]()
![]() ...{
            ...{
![]() return true;
                return true;
![]() }
            }
![]() set
            set 
![]()
![]() ...{
            ...{
![]() enableWaitPage = value;
                enableWaitPage = value;
![]() }
            }
![]() 
![]() }
        }
![]() 
![]() public NameValueCollection ReversePaths
        public NameValueCollection ReversePaths 
![]()
![]() ...{
        ...{
![]() get
            get 
![]()
![]() ...{
            ...{
![]() return reversePaths;
                return reversePaths;
![]() }
            }
![]() }
        }
![]() #endregion
        #endregion
![]() }
    }
![]() }
}
 
上面的Globals.ApplicationPath的代码是:
Globals.cs
![]() static public string ApplicationPath
static public string ApplicationPath 
![]()
![]() ...{
        ...{
![]() get
            get 
![]()
![]() ...{
            ...{
![]() string applicationPath = HttpContext.Current.Request.ApplicationPath;
                string applicationPath = HttpContext.Current.Request.ApplicationPath;
![]() if (applicationPath == "/")
                if (applicationPath == "/") 
![]()
![]() ...{
                ...{
![]() return string.Empty;
                    return string.Empty;
![]() }
                } 
![]() else
                else 
![]()
![]() ...{
                ...{
![]() return applicationPath;
                    return applicationPath;
![]() }
                }
![]() }
            }
![]() }
        }
 
在Globals.cs中加上如下代码:
![]() static public SiteUrls GetSiteUrls()
static public SiteUrls GetSiteUrls() 
![]()
![]() ...{
        ...{
![]() return new SiteUrls();
            return new SiteUrls();
![]() }
        }
 
然后在前端如此调用:
Response.Redirect (Globals.GetSiteUrls().News( 1,2));