Wednesday, 25 September 2013

Url Rewite

protected void Application_BeginRequest(Object sender, EventArgs e)
    {

 if (HttpContext.Current.Request.QueryString["isValid"] == null)
                {
                    if (HttpContext.Current.Request.UrlReferrer == null && HttpContext.Current.Request.QueryString.Count > 0 && !(HttpContext.Current.Request.Url.ToString().ToLower().Contains("fundpage.aspx")))
                    {
                        if (!HttpContext.Current.Request.Url.ToString().Contains("?2") && !HttpContext.Current.Request.Url.ToString().ToLower().Contains("?formcentre"))
                        {
                            Response.Redirect("~/errorpage.aspx");
                        }

                    }
                    else
                    {
                        // BusinessLogic.URLRewriter.ProcessRequest();
                        URLRewriterNew.URLRewriterNew.ProcessRequest();
                        URLTagRewriter.ProcessRequest();
                    }
                }
                else if (HttpContext.Current.Request.UrlReferrer == null)
                {
                    //Response.Redirect("~/default.aspx");
                }
}


=========================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Web;
using System.Data.SqlClient;
using System.Data;

using System.Xml;

public class URLTagRewriter : IConfigurationSectionHandler
{

    #region Private Variables
    // static AccessData _accessData = new AccessData();
    #endregion
    public static void ProcessRequest()
    {

        //string zSubst = oRewriter.GetSubstitution(HttpContext.Current.Request.Path);

        //Added by Deepak T to check the keyword

        if (HttpContext.Current.Request.Path.ToLower().Contains("aspx"))
        {
            //do nothing
            string zSubst = string.Empty;
            if (HttpContext.Current.Request.Url.AbsoluteUri.ToString().ToLower().Contains("?ptagid="))
            {
                string strPid = HttpContext.Current.Request.QueryString["ptagid"].ToString();
                int id = int.Parse(strPid);
                zSubst = ReadURLFromSql(id.ToString());
                if (zSubst.Length > 0)
                {
                    if (HttpContext.Current.Request.RequestType != "POST")
                    {
                        HttpContext.Current.Response.Redirect(zSubst);
                        //HttpContext.Current.RewritePath(zSubst);
                    }
                }

            }
            else
            {
                //HttpContext.Current.Response.Redirect("~/default.aspx");
            }

        }
        else if (HttpContext.Current.Request.Path.ToLower().Contains("."))
        {
            //do nothing
        }
        else
        {
            string zSubst = string.Empty;
            string strPid ="";
            zSubst = ReadURLFromSql();//ReadXML(HttpContext.Current.Server.MapPath("~/URL.xml"));

            if (zSubst.Length > 0)
            {
                HttpContext.Current.RewritePath(zSubst);
            }
            else
            {
                //HttpContext.Current.Response.Redirect("~/default.aspx");
                // throw new Exception("Page not found");
            }
        }
    }

    public static string ReadURLFromSql()
    {
        string strRequestPath = string.Empty;
        string dbPath = string.Empty;
        string strMatchPath = string.Empty;
        strRequestPath = HttpContext.Current.Request.Path; // got the requested path
        strMatchPath = strRequestPath.Substring(strRequestPath.LastIndexOf("/"), strRequestPath.Length - (strRequestPath.LastIndexOf("/")));
        if (strMatchPath.Contains("/"))
        {
            strMatchPath = strMatchPath.Remove(strMatchPath.IndexOf("/"), 1);
        }

        if (strMatchPath.Contains("'"))
        {
            int index = strMatchPath.IndexOf("'");
            index = index + 1;
            strMatchPath = strMatchPath.Insert(index, "'");
        }
        else
        {
            // strMatchPath = "7";
        }


        DataSet dsURL = new DataSet();


        //dsURL = GetFundPageLink("GetFundPageIDByLink","", strMatchPath);




        //if (dsURL != null && dsURL.Tables.Count > 0 && dsURL.Tables[0].Rows.Count > 0)
        //{
        //    dbPath = dsURL.Tables[0].Rows[0]["ID"].ToString();
      //  string strPid = HttpContext.Current.Request.QueryString["tagid"].ToString();
       //}
        //else
        //{
        //    dbPath = "";
        //}
  
        if (strMatchPath == "equity-funds")
        {
            dbPath = "1";
        }
        else if (strMatchPath == "debt-funds")
        {
            dbPath = "2";
        }
        else if (strMatchPath == "hybrid-funds")
        {
            dbPath = "3";
        }
        else if (strMatchPath == "tax-saving-funds")
        {
            dbPath = "7";
        }
        else if (strMatchPath == "long-term-funds")
        {
            dbPath = "18";
        } else
        {
            dbPath = "";
        }

        string str = string.Empty;
        if (dbPath != "")
            str = "~/mutual-funds-at-a-glance.aspx?tagID=" + dbPath + "";
        else
            str = "";

        return str;
    }

    public static string ReadURLFromSql(string fundID)
    {
        string strRequestPath = string.Empty;
        string dbPath = string.Empty;
        string strMatchPath = string.Empty;
        strRequestPath = HttpContext.Current.Request.Path; // got the requested path


        DataSet dsURL = new DataSet();

        //dsURL = GetFundPageLink("GetFundPageLink", fundID,"");

        if (fundID == "1")
        {
            dbPath = "equity-funds";
        }
        else if (fundID == "2")
        {
            dbPath = "debt-funds";
        }
        else if (fundID == "3")
        {
            dbPath = "hybrid-funds";
        }
        else if (fundID == "7")
        {
            dbPath = "tax-saving-funds";
        }
         else if (fundID == "18")
        {
            dbPath = "long-term-funds";
        }


        //if (dsURL!=null && dsURL.Tables.Count>0 &&  dsURL.Tables[0].Rows.Count > 0)
        //{
        //dbPath = "pramod";// dsURL.Tables[0].Rows[0]["FundPageLink"].ToString();
        // }

        string str = string.Empty;
        if (dbPath != "")
            str = "~/" + dbPath;
        else
            str = "";

        return str;
    }


    //public static DataSet GetFundPageLink(string strAction, string strFundID,string strFundLink)
    //{
    //    try
    //    {

    //        _accessData.Section = "FundPageLinks";
    //        object[] obj = _accessData.GetObject();
    //        obj[0] = strAction;
    //        obj[1] = strFundID;
    //        obj[2] = strFundLink;

    //        DataSet _ds = _accessData.getDataSet(obj);
    //        return _ds;
    //    }
    //    catch (Exception ex)
    //    {
    //        throw (ex);
    //    }
    //}



    #region Implementation of IConfigurationSectionHandler
    public object Create(object parent, object configContext, XmlNode section)
    {
        //_oRules = section;

        // TODO: Compile all Regular Expressions

        return this;
    }

    #endregion

}

No comments:

Post a Comment