using System; using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; namespace Umbraco.Web.Routing { /// /// Provides an implementation of that handles page identifiers. /// /// /// Handles /1234 where 1234 is the identified of a document. /// public class ContentFinderByIdPath : IContentFinder { private readonly IWebRoutingSection _webRoutingSection; public ContentFinderByIdPath() : this(UmbracoConfig.For.UmbracoSettings().WebRouting) { } public ContentFinderByIdPath(IWebRoutingSection webRoutingSection) { _webRoutingSection = webRoutingSection; } /// /// Tries to find and assign an Umbraco document to a PublishedContentRequest. /// /// The PublishedContentRequest. /// A value indicating whether an Umbraco document was found and assigned. public bool TryFindContent(PublishedContentRequest docRequest) { if (docRequest.RoutingContext.UmbracoContext != null && docRequest.RoutingContext.UmbracoContext.InPreviewMode == false && _webRoutingSection.DisableFindContentByIdPath) return false; IPublishedContent node = null; var path = docRequest.Uri.GetAbsolutePathDecoded(); var nodeId = -1; if (path != "/") // no id if "/" { var noSlashPath = path.Substring(1); if (!Int32.TryParse(noSlashPath, out nodeId)) nodeId = -1; if (nodeId > 0) { LogHelper.Debug("Id={0}", () => nodeId); node = docRequest.RoutingContext.UmbracoContext.ContentCache.GetById(nodeId); if (node != null) { docRequest.PublishedContent = node; LogHelper.Debug("Found node with id={0}", () => docRequest.PublishedContent.Id); } else { nodeId = -1; // trigger message below } } } if (nodeId == -1) LogHelper.Debug("Not a node id"); return node != null; } } }