using System;
using Umbraco.Core.Logging;
using Umbraco.Core.Models;
using Umbraco.Core;
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
{
///
/// 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)
{
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;
}
}
}