using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Sitecore.Configuration; using Sitecore.Data; using Sitecore.Data.Items; using Sitecore.Diagnostics; using Sitecore.Pipelines.HttpRequest; namespace Sitecore.Support.Pipelines.HttpRequest { public class AliasResolver : HttpRequestProcessor { public override void Process(HttpRequestArgs args) { Assert.ArgumentNotNull(args, "args"); if (!Settings.AliasesActive) { Tracer.Warning("Aliases are not active."); return; } Database database = Context.Database; if (database == null) { Tracer.Warning("There is no context database in AliasResover."); return; } Profiler.StartOperation("Resolve alias."); // Use the Context.Site.Name property to find the site specific aliases if (database.Aliases.Exists('/' + Context.Site.Name + args.LocalPath) && !this.ProcessItem(args)) { this.ProcessExternalUrl(args); } Profiler.EndOperation(); } private void ProcessExternalUrl(HttpRequestArgs args) { // Use the Context.Site.Name property to find the site specific aliases string targetUrl = Context.Database.Aliases.GetTargetUrl('/' + Context.Site.Name + args.LocalPath); if (targetUrl.Length > 0) { this.ProcessExternalUrl(targetUrl); } } private void ProcessExternalUrl(string path) { if (Context.Page.FilePath.Length > 0) { return; } Context.Page.FilePath = path; } private bool ProcessItem(HttpRequestArgs args) { // Use the Context.Site.Name property to find the site specific aliases ID targetID = Context.Database.Aliases.GetTargetID('/' + Context.Site.Name + args.LocalPath); if (!targetID.IsNull) { Item item = args.GetItem(targetID); if (item != null) { this.ProcessItem(args, item); } return true; } // Use the Context.Site.Name property to output porper information Tracer.Error("An alias for \"" + "/" + Context.Site.Name + args.LocalPath + "\" exists, but points to a non-existing item."); return false; } private void ProcessItem(HttpRequestArgs args, Item target) { if (Context.Item == null) { Context.Item = target; // Use the Context.Site.Name property to output proper information Tracer.Info(string.Concat(new object[] { "Using alias for \"", "/", Context.Site.Name, args.LocalPath, "\" which points to \"", target.ID, "\"" })); } } } }