Friday 4 May, 2012

Url rewriting using global.asax

To rewrite the url only using asax file like www.domain.com/items.aspx?ID=1
to 
www.domain.com/fackurl.aspx?ID=1 Add the below content in the Global.asax file

protected void Application_BeginRequest(object sender, EventArgs e)
{
    HttpContext myContext = HttpContext.Current;
    Regex rewrite_regex = new Regex(@"(.+)\/((.+)\.aspx)", RegexOptions.IgnoreCase);
    try
    {
        //see if we need to rewrite the URL
        Match match_rewrite = rewrite_regex.Match(myContext.Request.Path.ToString());
        if (match_rewrite.Groups[2].Captures[0].ToString() == "Default.aspx")
        {
            if (Request.QueryString["ID"] != null)
                myContext.RewritePath("~/Mail/Index.aspx?ID=" + Request.QueryString["ID"].ToString(), true);
        }
    }
    catch (Exception ex) { }
}

Tags: url rewriting in asp.net sample code, url rewriting using global.asax asp net, url rewriting with global.asax, url rewriting using global.asax asp net

No comments:

Post a Comment

Parsing JSON w/ @ symbol in it

To read the json response like bellow @ concatenated with attribute                             '{ "@id": 1001, "@name&q...