Пример #1
0
        protected virtual RedirectMapping FindMapping(string filePath)
        {
            RedirectMapping redirectMapping = null;

            List <RedirectMapping> .Enumerator enumerator = this.MappingsMap.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    RedirectMapping current = enumerator.Current;
                    if ((current.IsRegex || !(current.Pattern == filePath)) && (!current.IsRegex || !current.Regex.IsMatch(filePath)))
                    {
                        continue;
                    }
                    redirectMapping = current;
                    return(redirectMapping);
                }
                return(null);
            }
            catch (Exception)
            {
            }
            finally
            {
                ((IDisposable)enumerator).Dispose();
            }
            return(redirectMapping);
        }
Пример #2
0
        public override void ProcessRequest(HttpRequestArgs args)
        {
            //Item item = Context.Item;

            if (args == null || args.HttpContext == null)
            {
                return;
            }

            var context = args.HttpContext;

            if (Context.Site == null || this.IsFile(Context.Request.FilePath))
            {
                return;
            }

            string str = this.EnsureSlashes(Context.Request.FilePath.ToLower());



            RedirectMapping resolvedMapping = this.GetResolvedMapping(str);

            bool flag = resolvedMapping != null;

            if (resolvedMapping == null)
            {
                resolvedMapping = this.FindMapping(str);
            }


            if (resolvedMapping != null && !flag)
            {
                var item = GetCache <Dictionary <string, RedirectMapping> >(ResolvedMappingsPrefix)
                           ?? new Dictionary <string, RedirectMapping>();

                item[str] = resolvedMapping;

                SetCache(ResolvedMappingsPrefix, item);
            }


            if (resolvedMapping != null && HttpContext.Current != null)
            {
                string targetUrl = this.GetTargetUrl(resolvedMapping, str);
                if (resolvedMapping.RedirectType == RedirectType.Redirect301)
                {
                    this.Redirect301(HttpContext.Current.Response, targetUrl);
                }
                if (resolvedMapping.RedirectType == RedirectType.Redirect302)
                {
                    HttpContext.Current.Response.Redirect(targetUrl, true);
                }
                if (resolvedMapping.RedirectType == RedirectType.ServerTransfer)
                {
                    HttpContext.Current.Server.TransferRequest(targetUrl);
                }
            }
        }
Пример #3
0
        protected virtual string GetTargetUrl(RedirectMapping mapping, string input)
        {
            string target = mapping.Target;

            if (mapping.IsRegex)
            {
                target = mapping.Regex.Replace(input.TrimEnd(new char[] { '/' }), target);
            }
            if (mapping.PreserveQueryString)
            {
                target = string.Concat(target.TrimEnd(new char[] { '/' }), HttpContext.Current.Request.Url.Query);
            }
            if (!string.IsNullOrEmpty(Context.Site.VirtualFolder))
            {
                target = string.Concat(StringUtil.EnsurePostfix('/', Context.Site.VirtualFolder), target.TrimStart(new char[] { '/' }));
            }
            return(target);
        }