Question : Read XML file in global.asax to redirect page request to correct page

I have created a xml file that has a vanity url abd the redirect url. I want global asax to read this file to redirect the page request to the appropiate page. SO for example if someone types in http://www.company.com/products have that be redirected to http://www.company.com/siteproducts/products.aspx. below is some code I wrote.

  <?xml version="1.0" encoding="UTF-8" ?>
<redirectlinks>
<link>
  <vanitylink>/products/</vanitylink>
  <redirectlink>/siteproducts/products.aspx?WT.MC_ID=23</redirectlink>
</link>
</redirectlinks>

global.asax

public void Application_BeginRequest()
{
    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"];
        strRedirectLink = Request.ApplicationPath + strRedirectLink;
        Context.RewritePath(strRedirectLink);
    }
}

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;
}


Any ideas on how to get this to work is greatly appreciated.

Answer : Read XML file in global.asax to redirect page request to correct page

You can use one of the pre-built modules instead of writing it yourself as it is tedious and error prone.

http://urlrewriter.net/

The updated code is also posted below.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
    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);
        }
    }
Random Solutions  
 
programming4us programming4us