Exemple #1
0
        public IHttpHandler GetHandler(HttpContext context, string requestType, string url1, string pathTranslated)
        {
            string url     = context.Request.Url.AbsolutePath;
            string keycach = context.Request.RawUrl.ToString();
            string html    = context.Cache[keycach] != null ? context.Cache[keycach].ToString() : String.Empty;
            string newFilePath;

            if (!String.IsNullOrEmpty(html))
            {
                context.Response.Write(html);
                newFilePath = "/blank.aspx";
                return(PageParser.GetCompiledPageInstance(newFilePath, context.Server.MapPath(newFilePath), context));
            }

            string rewrite = "";

            if (HttpContext.Current.Request.Url.Query != String.Empty)
            {
                if (context.Request.Url.Query.Length > 0)
                {
                    context.Items["VirtualUrl"] = context.Request.Path + context.Request.Url.Query;
                }
            }
            if (context.Items["VirtualUrl"] == null)
            {
                context.Items["VirtualUrl"] = context.Request.Path;
            }

            RewriteRules rewriteRules = RewriteRules.GetCurrentRewriteRules();

            rewrite = rewriteRules.GetMatchingRewrite(url);


            if (!string.IsNullOrEmpty(rewrite))
            {
                context.RewritePath("~" + rewrite);
            }
            else
            {
                {
                    rewrite = context.Request.Path + context.Request.Url.Query;
                }
            }

            newFilePath = rewrite != null && rewrite.IndexOf("?") > 0 ? rewrite.Substring(0, rewrite.IndexOf("?")) : rewrite;

            if (string.IsNullOrEmpty(newFilePath))
            {
                newFilePath = "/blank.aspx";
            }

            try
            {
                return(PageParser.GetCompiledPageInstance(newFilePath, context.Server.MapPath(newFilePath), context));
            }
            catch (Exception ex)
            {
                return(PageParser.GetCompiledPageInstance("/404.aspx", context.Server.MapPath("/blank.aspx"), context));
            }
        }
Exemple #2
0
        public static RewriteRules GetCurrentRewriteRules()
        {
            string cacheName = "CommonConfiguration_RewriteRules_TTVH";

            if (null != HttpContext.Current.Cache[cacheName])
            {
                try
                {
                    return((RewriteRules)HttpContext.Current.Cache[cacheName]);
                }
                catch
                {
                    return(new RewriteRules());
                }
            }
            else
            {
                try
                {
                    string      configFilePath = HttpContext.Current.Server.MapPath("/Config/RewriteRules.config"); //@"D:\Running projects\VC Corporation\Dantri\Dantri.Cached\CacheSettings.config";
                    XmlDocument xmlDoc         = new XmlDocument();
                    xmlDoc.Load(configFilePath);

                    RewriteRules rules = new RewriteRules();

                    XmlNodeList nlstRules = xmlDoc.DocumentElement.SelectNodes("//rules/rule");

                    for (int i = 0; i < nlstRules.Count; i++)
                    {
                        RewriteRule rule = new RewriteRule();
                        rule.Url     = nlstRules[i].SelectSingleNode("url").InnerText;
                        rule.Rewrite = nlstRules[i].SelectSingleNode("rewrite").InnerText;

                        rules.List.Add(rule);
                    }

                    XmlNode nodeFileSettingCacheExpire = xmlDoc.DocumentElement.SelectSingleNode("//Configuration/RewriteRulesFile");
                    long    fileSettingCacheExpire     = Lib.Object2Long(nodeFileSettingCacheExpire.Attributes["cacheExpire"].Value);
                    if (fileSettingCacheExpire <= 0)
                    {
                        fileSettingCacheExpire = 3600;// default 1h
                    }

                    CacheDependency fileDependency = new CacheDependency(configFilePath);
                    HttpContext.Current.Cache.Insert(cacheName, rules, fileDependency, DateTime.Now.AddSeconds(fileSettingCacheExpire), TimeSpan.Zero, CacheItemPriority.Normal, null);

                    return(rules);
                }
                catch
                {
                    return(new RewriteRules());
                }
            }
        }