2018-09-14 00:44:36 +10:00
using System.Globalization ;
2020-09-21 09:52:58 +02:00
using Microsoft.Extensions.Logging ;
2021-01-06 17:04:35 +11:00
using Microsoft.Extensions.Options ;
using Umbraco.Core ;
using Umbraco.Core.Configuration.Models ;
using Umbraco.Core.Models.PublishedContent ;
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
{
2020-09-21 09:52:58 +02:00
private readonly ILogger < ContentFinderByIdPath > _logger ;
2020-02-28 11:15:25 +01:00
private readonly IRequestAccessor _requestAccessor ;
2021-01-06 17:04:35 +11:00
private readonly IUmbracoContextAccessor _umbracoContextAccessor ;
2020-08-21 14:52:47 +01:00
private readonly WebRoutingSettings _webRoutingSettings ;
2020-02-10 13:09:26 +01:00
2021-01-06 17:04:35 +11:00
/// <summary>
/// Initializes a new instance of the <see cref="ContentFinderByIdPath"/> class.
/// </summary>
public ContentFinderByIdPath (
IOptions < WebRoutingSettings > webRoutingSettings ,
ILogger < ContentFinderByIdPath > logger ,
IRequestAccessor requestAccessor ,
IUmbracoContextAccessor umbracoContextAccessor )
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 ) ) ;
2021-01-06 17:04:35 +11:00
_requestAccessor = requestAccessor ? ? throw new System . ArgumentNullException ( nameof ( requestAccessor ) ) ;
_umbracoContextAccessor = umbracoContextAccessor ? ? throw new System . ArgumentNullException ( nameof ( umbracoContextAccessor ) ) ;
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>
2021-01-06 17:04:35 +11:00
public bool TryFindContent ( IPublishedRequestBuilder frequest )
2018-06-29 19:52:40 +02:00
{
2021-01-06 17:04:35 +11:00
IUmbracoContext umbCtx = _umbracoContextAccessor . UmbracoContext ;
if ( umbCtx = = null | | ( umbCtx ! = null & & umbCtx . InPreviewMode = = false & & _webRoutingSettings . DisableFindContentByIdPath ) )
{
2018-06-29 19:52:40 +02:00
return false ;
2021-01-06 17:04:35 +11:00
}
2018-06-29 19:52:40 +02:00
IPublishedContent node = null ;
var path = frequest . Uri . GetAbsolutePathDecoded ( ) ;
var nodeId = - 1 ;
2021-01-06 17:04:35 +11:00
// no id if "/"
if ( path ! = "/" )
2018-06-29 19:52:40 +02:00
{
var noSlashPath = path . Substring ( 1 ) ;
if ( int . TryParse ( noSlashPath , out nodeId ) = = false )
2021-01-06 17:04:35 +11:00
{
2018-06-29 19:52:40 +02:00
nodeId = - 1 ;
2021-01-06 17:04:35 +11:00
}
2018-06-29 19:52:40 +02:00
if ( nodeId > 0 )
{
2020-09-16 10:24:05 +02:00
_logger . LogDebug ( "Id={NodeId}" , nodeId ) ;
2021-01-06 17:04:35 +11:00
node = umbCtx . 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" ) ;
2021-01-06 17:04:35 +11: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
{
2021-01-06 17:04:35 +11: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
frequest . SetCulture ( CultureInfo . GetCultureInfo ( cultureFromQuerystring ) ) ;
2018-09-14 00:44:36 +10:00
}
2021-01-06 17:04:35 +11:00
frequest . SetPublishedContent ( node ) ;
_logger . LogDebug ( "Found node with id={PublishedContentId}" , node . Id ) ;
2018-06-29 19:52:40 +02:00
}
else
{
nodeId = - 1 ; // trigger message below
}
}
}
if ( nodeId = = - 1 )
2021-01-06 17:04:35 +11:00
{
2020-09-16 10:24:05 +02:00
_logger . LogDebug ( "Not a node id" ) ;
2021-01-06 17:04:35 +11:00
}
2018-06-29 19:52:40 +02:00
return node ! = null ;
}
}
}