Ejemplo n.º 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");
        }
Ejemplo n.º 2
0
        /// <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));
        }