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