Пример #1
0
        /// <summary>
        /// This method is called during the module's BeginRequest event.
        /// </summary>
        /// <param name="requestedRawUrl">The RawUrl being requested (includes path and querystring).</param>
        /// <param name="app">The HttpApplication instance.</param>
        protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
        {
            // log information to the Trace object.
            app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");

            // get the configuration rules
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            // iterate through each rule...
            for (int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

                // Create a regex (note that IgnoreCase is set...)
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                // See if a match is found
                if (re.IsMatch(requestedPath))
                {
                    // match found - do any replacement needed
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

                    // log rewriting information to the Trace object
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);

                    // Rewrite the URL
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;              // exit the for loop
                }
            }

            // Log information to the Trace object
            app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
        }
Пример #2
0
        protected override void Rewrite(string requestedPath, System.Web.HttpApplication app)
        {
            // get the configuration rules 获得配置规则
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            // iterate through each rule... 遍历每个规则...
            for (int i = 0; i < rules.Count; i++)
            {
                // get the pattern to look for, and Resolve the Url (convert ~ into the appropriate directory) 获得要查找的模式,并且解析 Url(转换为相应的目录)
                string lookFor = "^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$";

                // Create a regex (note that IgnoreCase is set...) 创建 regex(请注意,已设置 IgnoreCase...)
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                //See if a match is found 查看是否找到了匹配的规则

                if (re.IsMatch(requestedPath))
                {
                    //match found - do any replacement needed 找到了匹配的规则 -- 进行必要的替换
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, re.Replace(requestedPath, rules[i].SendTo));

                    // Rewrite the URL 重写 URL
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break; //exit the for loop 退出 For 循环
                }
            }
        }
Пример #3
0
        public static void  ReloadConfig()
        {
            Rules = RewriterConfiguration.GetConfig().Rules;
            CryptoHelper cr = new CryptoHelper();

            for (int i = 0; i < Rules.Count; i++)
            {
                //---------------解密--------------------------
                Rules[i].LookFor = cr.Decrypt(Rules[i].LookFor);
                Rules[i].SendTo  = cr.Decrypt(Rules[i].SendTo);
                //---------------完毕-------------------------
            }
        }
        /// <summary>
        /// GetHandler is executed by the ASP.NET pipeline after the associated HttpModules have run.  The job of
        /// GetHandler is to return an instance of an HttpHandler that can process the page.
        /// </summary>
        /// <param name="context">The HttpContext for this request.</param>
        /// <param name="requestType">The HTTP data transfer method (<b>GET</b> or <b>POST</b>)</param>
        /// <param name="url">The RawUrl of the requested resource.</param>
        /// <param name="pathTranslated">The physical path to the requested resource.</param>
        /// <returns>An instance that implements IHttpHandler; specifically, an HttpHandler instance returned
        /// by the <b>PageParser</b> class, which is the same class that the default ASP.NET PageHandlerFactory delegates
        /// to.</returns>
        public virtual IHttpHandler GetHandler(HttpContext context, string requestType, string url, string pathTranslated)
        {
            // log info to the Trace object...
            //	context.Trace.Write("RewriterFactoryHandler", "Entering RewriterFactoryHandler");

            string sendToUrl = url;
            string filePath  = pathTranslated;

            // get the configuration rules
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            // iterate through the rules
            for (int i = 0; i < rules.Count; i++)
            {
                // Get the pattern to look for (and resolve its URL)
                string lookFor = "^" + RewriterUtils.ResolveUrl(context.Request.ApplicationPath, rules[i].LookFor) + "$";

                // Create a regular expression object that ignores case...
                Regex re = new Regex(lookFor, RegexOptions.IgnoreCase);

                // Check to see if we've found a match
                if (re.IsMatch(url))
                {
                    // do any replacement needed
                    sendToUrl = RewriterUtils.ResolveUrl(context.Request.ApplicationPath, re.Replace(url, rules[i].SendTo));

                    // log info to the Trace object...
                    //	context.Trace.Write("RewriterFactoryHandler", "Found match, rewriting to " + sendToUrl);

                    // Rewrite the path, getting the querystring-less url and the physical file path
                    string sendToUrlLessQString;

                    RewriterUtils.RewriteUrl(context, sendToUrl, out sendToUrlLessQString, out filePath);


                    // return a compiled version of the page
                    //	context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler");	// log info to the Trace object...
                    return(PageParser.GetCompiledPageInstance(sendToUrlLessQString, filePath, context));
                }
            }


            // if we reached this point, we didn't find a rewrite match
            //	context.Trace.Write("RewriterFactoryHandler", "Exiting RewriterFactoryHandler");
            // log info to the Trace object...
            return(PageParser.GetCompiledPageInstance(url, filePath, context));
        }
Пример #5
0
        protected override void Rewrite(string requestedPath, HttpApplication app)
        {
            app.Context.Trace.Write("ModuleRewriter", "Entering ModuleRewriter");
            RewriterRuleCollection rules = RewriterConfiguration.GetConfig().Rules;

            for (int i = 0; i < rules.Count; i++)
            {
                Regex regex = new Regex("^" + RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, rules[i].LookFor) + "$", RegexOptions.IgnoreCase);
                if (regex.IsMatch(requestedPath))
                {
                    string sendToUrl = RewriterUtils.ResolveUrl(app.Context.Request.ApplicationPath, regex.Replace(requestedPath, rules[i].SendTo));
                    app.Context.Trace.Write("ModuleRewriter", "Rewriting URL to " + sendToUrl);
                    RewriterUtils.RewriteUrl(app.Context, sendToUrl);
                    break;
                }
            }
            app.Context.Trace.Write("ModuleRewriter", "Exiting ModuleRewriter");
        }