Example #1
0
        /// <summary>
        /// Handles the URL Rewriting for the application
        /// </summary>
        /// <param name="context">Http context</param>
        public static void BeginRequest(HttpContext context)
        {
            try {
                string path = context.Request.Path.Substring(context.Request.ApplicationPath.Length > 1 ?
                                                             context.Request.ApplicationPath.Length : 0);

                string[] args = path.Split(new char[] { '/' }).Subset(1);

                if (args.Length > 0)
                {
                    int pos = 0;

                    // Ensure database
                    if (args[0] == "" && SysParam.GetByName("SITE_VERSION") == null)
                    {
                        context.Response.Redirect("~/manager", false);
                        context.Response.EndClean();
                    }

                    // Check for culture prefix
                    if (Cultures.ContainsKey(args[0]))
                    {
                        System.Threading.Thread.CurrentThread.CurrentCulture       =
                            System.Threading.Thread.CurrentThread.CurrentUICulture = Cultures[args[0]];
                        pos = 1;
                    }
                    else
                    {
                        var def = (GlobalizationSection)WebConfigurationManager.GetSection("system.web/globalization");
                        if (def != null)
                        {
                            System.Threading.Thread.CurrentThread.CurrentCulture       =
                                System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(def.UICulture);
                        }
                    }

                    var handled = false;

                    // Find the correct request handler
                    foreach (var hr in Application.Current.Handlers)
                    {
                        if (hr.UrlPrefix.ToLower() == args[pos].ToLower())
                        {
                            if (hr.Id != "PERMALINK" || !PrefixlessPermalinks)
                            {
                                // Don't execute permalink routing in passive mode
                                if ((hr.Id != "PERMALINK" && hr.Id != "STARTPAGE") || !Config.PassiveMode)
                                {
                                    // Execute the handler
                                    hr.Handler.HandleRequest(context, args.Subset(pos + 1));
                                    handled = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (!handled && args[pos].ToLower() == "res.ashx")
                    {
                        Application.Current.Resources.HandleRequest(context, args.Subset(pos + 1));
                        handled = true;
                    }

                    // If no handler was found and we are using prefixless permalinks,
                    // route traffic to the permalink handler.
                    if (!Config.PassiveMode)
                    {
                        if (!handled && PrefixlessPermalinks && args[0].ToLower() != "manager" && String.IsNullOrEmpty(context.Request["permalink"]))
                        {
                            if (Permalink.GetByName(Config.SiteTreeNamespaceId, args[0]) != null || Permalink.GetByName(Config.DefaultNamespaceId, args[0]) != null)
                            {
                                var handler = new PermalinkHandler();
                                handler.HandleRequest(context, args);
                            }
                        }
                    }
                }
            } catch (ThreadAbortException) {
                // We simply swallow this exception as we don't want unhandled
                // exceptions flying around causing the app pool to die.
            } catch (Exception e) {
                // One catch to rule them all, and in the log file bind them.
                Application.Current.LogProvider.Error("WebPiranha.BeginRequest", "Unhandled exception", e);
                context.Response.StatusCode = 500;
                context.Response.EndClean();
            }
        }
Example #2
0
        /// <summary>
        /// Handles the URL Rewriting for the application
        /// </summary>
        /// <param name="context">Http context</param>
        public static void BeginRequest(HttpContext context)
        {
            string path = context.Request.Path.Substring(context.Request.ApplicationPath.Length > 1 ?
                                                         context.Request.ApplicationPath.Length : 0);

            string[] args = path.Split(new char[] { '/' }).Subset(1);

            if (args.Length > 0)
            {
                int pos = 0;

                // Ensure database
                if (args[0] == "" && SysParam.GetByName("SITE_VERSION") == null)
                {
                    context.Response.Redirect("~/manager");
                }

                // Check for culture prefix
                if (Cultures.ContainsKey(args[0]))
                {
                    System.Threading.Thread.CurrentThread.CurrentUICulture = Cultures[args[0]];
                    pos = 1;
                }
                else
                {
                    var def = (GlobalizationSection)WebConfigurationManager.GetSection("system.web/globalization");
                    if (def != null)
                    {
                        System.Threading.Thread.CurrentThread.CurrentUICulture = new CultureInfo(def.UICulture);
                    }
                }

                var handled = false;

                // Find the correct request handler
                foreach (RequestHandlerRegistration hr in Handlers.Values)
                {
                    if (hr.UrlPrefix.ToLower() == args[pos].ToLower())
                    {
                        if (hr.Id != "PERMALINK" || !PrefixlessPermalinks)
                        {
                            // Execute the handler
                            hr.Handler.HandleRequest(context, args.Subset(pos + 1));
                            handled = false;
                            break;
                        }
                    }
                }

                // If no handler was found and we are using prefixless permalinks,
                // route traffic to the permalink handler.
                if (!handled && PrefixlessPermalinks && args[0].ToLower() != "manager")
                {
                    if (Permalink.GetByName(Config.SiteTreeNamespaceId, args[0]) != null)
                    {
                        var handler = new PermalinkHandler();
                        handler.HandleRequest(context, args);
                    }
                }
            }
        }