Files
Umbraco-CMS/src/Umbraco.Core/Routing/ContentFinderByIdPath.cs

102 lines
4.0 KiB
C#
Raw Normal View History

2018-09-14 00:44:36 +10:00
using System.Globalization;
using Microsoft.Extensions.Logging;
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
{
private readonly ILogger<ContentFinderByIdPath> _logger;
private readonly IRequestAccessor _requestAccessor;
private readonly IUmbracoContextAccessor _umbracoContextAccessor;
private readonly WebRoutingSettings _webRoutingSettings;
/// <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
{
_webRoutingSettings = webRoutingSettings.Value ?? throw new System.ArgumentNullException(nameof(webRoutingSettings));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
_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>
/// Tries to find and assign an Umbraco document to a <c>PublishedRequest</c>.
2018-06-29 19:52:40 +02:00
/// </summary>
/// <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>
public bool TryFindContent(IPublishedRequestBuilder frequest)
2018-06-29 19:52:40 +02: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;
}
2018-06-29 19:52:40 +02:00
IPublishedContent node = null;
var path = frequest.Uri.GetAbsolutePathDecoded();
var nodeId = -1;
// 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)
{
2018-06-29 19:52:40 +02:00
nodeId = -1;
}
2018-06-29 19:52:40 +02:00
if (nodeId > 0)
{
2020-09-16 10:24:05 +02:00
_logger.LogDebug("Id={NodeId}", nodeId);
node = umbCtx.Content.GetById(nodeId);
2018-06-29 19:52:40 +02:00
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))
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
frequest.SetCulture(CultureInfo.GetCultureInfo(cultureFromQuerystring));
2018-09-14 00:44:36 +10: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)
{
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;
}
}
}