2019-04-30 20:13:59 +01:00
using Umbraco.Core.Logging ;
2018-06-29 19:52:40 +02:00
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
using Umbraco.Core.Configuration.UmbracoSettings ;
using Umbraco.Core.Models.PublishedContent ;
2018-09-14 00:44:36 +10:00
using System.Globalization ;
2020-08-21 14:52:47 +01:00
using Umbraco.Core.Configuration.Models ;
using Microsoft.Extensions.Options ;
2018-06-29 19:52:40 +02:00
namespace Umbraco.Web.Routing
{
/// <summary>
/// Provides an implementation of <see cref="IContentFinder"/> that handles page identifiers.
/// </summary>
/// <remarks>
/// <para>Handles <c>/1234</c> where <c>1234</c> is the identified of a document.</para>
/// </remarks>
public class ContentFinderByIdPath : IContentFinder
{
private readonly ILogger _logger ;
2020-02-28 11:15:25 +01:00
private readonly IRequestAccessor _requestAccessor ;
2020-08-21 14:52:47 +01:00
private readonly WebRoutingSettings _webRoutingSettings ;
2020-02-10 13:09:26 +01:00
2020-08-23 23:36:48 +02:00
public ContentFinderByIdPath ( IOptions < WebRoutingSettings > webRoutingSettings , ILogger logger , IRequestAccessor requestAccessor )
2018-06-29 19:52:40 +02:00
{
2020-08-21 14:52:47 +01:00
_webRoutingSettings = webRoutingSettings . Value ? ? throw new System . ArgumentNullException ( nameof ( webRoutingSettings ) ) ;
2018-04-24 13:07:18 +10:00
_logger = logger ? ? throw new System . ArgumentNullException ( nameof ( logger ) ) ;
2020-02-28 11:15:25 +01:00
_requestAccessor = requestAccessor ;
2018-06-29 19:52:40 +02:00
}
/// <summary>
2019-04-30 20:13:59 +01:00
/// Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
2018-06-29 19:52:40 +02:00
/// </summary>
2019-04-30 20:13:59 +01:00
/// <param name="frequest">The <c>PublishedRequest</c>.</param>
2018-06-29 19:52:40 +02:00
/// <returns>A value indicating whether an Umbraco document was found and assigned.</returns>
2020-02-10 19:23:42 +01:00
public bool TryFindContent ( IPublishedRequest frequest )
2018-06-29 19:52:40 +02:00
{
if ( frequest . UmbracoContext ! = null & & frequest . UmbracoContext . InPreviewMode = = false
2020-03-12 09:52:34 +01:00
& & _webRoutingSettings . DisableFindContentByIdPath )
2018-06-29 19:52:40 +02:00
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 )
{
2020-09-16 10:24:05 +02:00
_logger . LogDebug ( "Id={NodeId}" , nodeId ) ;
2019-04-22 18:14:03 +02:00
node = frequest . UmbracoContext . Content . GetById ( nodeId ) ;
2018-06-29 19:52:40 +02:00
if ( node ! = null )
{
2020-02-28 11:15:25 +01:00
var cultureFromQuerystring = _requestAccessor . GetQueryStringValue ( "culture" ) ;
2018-09-14 00:44:36 +10:00
//if we have a node, check if we have a culture in the query string
2020-02-28 11:15:25 +01:00
if ( ! string . IsNullOrEmpty ( cultureFromQuerystring ) )
2018-09-14 00:44:36 +10:00
{
//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
2020-02-28 11:15:25 +01:00
frequest . Culture = CultureInfo . GetCultureInfo ( cultureFromQuerystring ) ;
2018-09-14 00:44:36 +10:00
}
2018-06-29 19:52:40 +02:00
frequest . PublishedContent = node ;
2020-09-16 10:24:05 +02:00
_logger . LogDebug ( "Found node with id={PublishedContentId}" , frequest . PublishedContent . Id ) ;
2018-06-29 19:52:40 +02:00
}
else
{
nodeId = - 1 ; // trigger message below
}
}
}
if ( nodeId = = - 1 )
2020-09-16 10:24:05 +02:00
_logger . LogDebug ( "Not a node id" ) ;
2018-06-29 19:52:40 +02:00
return node ! = null ;
}
}
}