using Umbraco.Core.Logging; using Umbraco.Core; using Umbraco.Core.Configuration; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Models.PublishedContent; using System.Globalization; 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 ILogger _logger; private readonly IRequestAccessor _requestAccessor; private readonly IWebRoutingSettings _webRoutingSettings; public ContentFinderByIdPath(IWebRoutingSettings webRoutingSettings, ILogger logger, IRequestAccessor requestAccessor) { _webRoutingSettings = webRoutingSettings ?? throw new System.ArgumentNullException(nameof(webRoutingSettings)); _logger = logger ?? throw new System.ArgumentNullException(nameof(logger)); _requestAccessor = requestAccessor; } /// /// Tries to find and assign an Umbraco document to a PublishedRequest. /// /// The PublishedRequest. /// A value indicating whether an Umbraco document was found and assigned. public bool TryFindContent(IPublishedRequest frequest) { if (frequest.UmbracoContext != null && frequest.UmbracoContext.InPreviewMode == false && _webRoutingSettings.DisableFindContentByIdPath) return false; IPublishedContent node = null; var path = frequest.Uri.GetAbsolutePathDecoded(); var nodeId = -1; if (path != "/") // no id if "/" { var noSlashPath = path.Substring(1); if (int.TryParse(noSlashPath, out nodeId) == false) nodeId = -1; if (nodeId > 0) { _logger.Debug("Id={NodeId}", nodeId); node = frequest.UmbracoContext.Content.GetById(nodeId); if (node != null) { var cultureFromQuerystring = _requestAccessor.GetQueryStringValue("culture"); //if we have a node, check if we have a culture in the query string if (!string.IsNullOrEmpty(cultureFromQuerystring)) { //we're assuming it will match a culture, if an invalid one is passed in, an exception will throw (there is no TryGetCultureInfo method), i think this is ok though frequest.Culture = CultureInfo.GetCultureInfo(cultureFromQuerystring); } frequest.PublishedContent = node; _logger.Debug("Found node with id={PublishedContentId}", frequest.PublishedContent.Id); } else { nodeId = -1; // trigger message below } } } if (nodeId == -1) _logger.Debug("Not a node id"); return node != null; } } }