void Application_BeginRequest(Object sender, EventArgs e)
{
RewriteUrl();
}
public DataTable GetVanityLinks()
{
DataSet dstVanityLinks = null;
DataTable dtblVanityLinks = null;
//dtblVanityLinks = Context.Cache("vanitylinks");
if (dtblVanityLinks == null)
{
dstVanityLinks = new DataSet();
dstVanityLinks.ReadXml(Server.MapPath("VanityURLRedirects.xml"));
dtblVanityLinks = dstVanityLinks.Tables[0];
Context.Cache.Insert("vanitylinks", dtblVanityLinks, new CacheDependency(Server.MapPath("VanityURLRedirects.xml")));
}
return dtblVanityLinks;
}
public void RewriteUrl()
{
DataTable dtblVanityLinks = null;
string strThisUrl = null;
string strSelect = null;
DataRow[] arrMatches = null;
string strRedirectLink = null;
dtblVanityLinks = GetVanityLinks();
strThisUrl = Request.Path.ToLower();
if (Request.ApplicationPath != "/")
{
strThisUrl = strThisUrl.Remove(0, Request.ApplicationPath.Length);
}
strSelect = "vanitylink = '" + strThisUrl + "'";
arrMatches = dtblVanityLinks.Select(strSelect, "vanitylink");
if (arrMatches.Length > 0)
{
strRedirectLink = arrMatches[0]["redirectlink"].ToString(); ;
strRedirectLink = Request.ApplicationPath + strRedirectLink;
Context.RewritePath(strRedirectLink);
}
}
|