public override void Process(HttpRequestArgs args) { Assert.ArgumentNotNull(args, "args"); string url = args.Context.Request.Url.PathAndQuery; // if ItemResolver failed to find an Item if (((Context.Item == null) && string.IsNullOrEmpty(Context.Page.FilePath)) || url.Contains("404;")) { // extract 404 url if (url.Contains("404;")) { url = Regex.Replace(HttpUtility.UrlDecode(url), ".*404;(.*)", "$1"); } // find an Item Redirect RedirectEntry redirect = RedirectManager.GetRedirect(Sitecore.Context.GetSiteName(), url); if (redirect != null) { UrlString newUrl = null; Item item = Context.Database.GetItem(new ID(redirect.ItemID)); if (item != null) { UrlOptions options = LinkManager.GetDefaultUrlOptions(); newUrl = new UrlString(LinkManager.GetItemUrl(item, options)); } if (newUrl != null && !string.IsNullOrEmpty(redirect.QueryString)) { var qsParameters = StringUtil.ParseNameValueCollection(redirect.QueryString, '&', '='); foreach (string key in qsParameters.AllKeys) { newUrl[key] = qsParameters[key]; } } // avoid looping if (newUrl != null && string.Compare(url, newUrl.ToString(), true) != 0) { args.AbortPipeline(); RedirectUtils.Do301Redirect(args.Context.Response, newUrl.ToString()); } } // no Item redirect found, try a NotFoundRule else if (url != null) { NotFoundRule rule = RedirectManager.ResolveRedirectRule(Sitecore.Context.GetSiteName(), url); if (rule != null) { ExecuteRule(url, rule, args); } } } }
public override void Execute(CommandContext context) { Assert.ArgumentNotNull(context, "context"); ClientPipelineArgs args = new ClientPipelineArgs(); ListString list = new ListString(context.Parameters["entryids"]); bool isNew = context.Parameters["new"] == "1"; if (list.Items.Any() && !isNew) { Guid entryId = MainUtil.GetGuid(list.Items.FirstOrDefault(), Guid.Empty); if (entryId != Guid.Empty) { RedirectEntry entry = RedirectManager.GetRedirect(entryId); if (entry != null) { args.Parameters["entryid"] = entryId.ToString(); args.Parameters["oldpath"] = entry.OldPath; args.Parameters["itemid"] = entry.ItemID.ToString(); args.Parameters["querystring"] = entry.QueryString; args.Parameters["site"] = entry.Site; } } } ContinuationManager current = ContinuationManager.Current; if (current != null) { current.Start(this, "Run", args); } else { Context.ClientPage.Start(this, "Run", args); } }
protected void Run(ClientPipelineArgs args) { Assert.ArgumentNotNull(args, "args"); if (args.IsPostBack) { if (args.HasResult && args.Result != "undefined") { UrlString results = new UrlString(args.Result); Database db = Sitecore.Context.ContentDatabase; Guid entryId = MainUtil.GetGuid(args.Parameters["entryid"], Guid.Empty); Item item = db.GetItem(HttpUtility.UrlDecode(results["itemid"])); string oldPath = HttpUtility.UrlDecode(results["oldpath"]); string querystring = HttpUtility.UrlDecode(results["querystring"]); string site = HttpUtility.UrlDecode(results["site"]); if (item != null) { string siteName = site;// RedirectManager.ResolveSiteName(item); if (entryId != Guid.Empty) { RedirectEntry entry = RedirectManager.GetRedirect(entryId); if (entry != null) { entry.ItemID = item.ID.ToGuid(); entry.OldPath = oldPath; entry.QueryString = querystring; entry.Site = siteName; RedirectManager.UpdateRedirect(entry); } } else { RedirectManager.CreateRedirect(siteName, oldPath, item.ID, querystring); } } Client.AjaxScriptManager.Dispatch("redirects:refresh"); } } else { UrlString url = new UrlString(UIUtil.GetUri("control:EditRedirectForm")); if (!string.IsNullOrEmpty(args.Parameters["itemid"])) { url["itemid"] = args.Parameters["itemid"]; } if (!string.IsNullOrEmpty(args.Parameters["oldpath"])) { url["oldpath"] = args.Parameters["oldpath"]; } if (!string.IsNullOrEmpty(args.Parameters["querystring"])) { url["querystring"] = args.Parameters["querystring"]; } if (!string.IsNullOrEmpty(args.Parameters["site"])) { url["site"] = args.Parameters["site"]; } SheerResponse.ShowModalDialog(url.ToString(), true); args.WaitForPostBack(); } }