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

88 lines
3.4 KiB
C#
Raw Normal View History

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;
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;
private readonly IRequestAccessor _requestAccessor;
private readonly WebRoutingSettings _webRoutingSettings;
public ContentFinderByIdPath(IOptions<WebRoutingSettings> webRoutingSettings, ILogger logger, IRequestAccessor requestAccessor)
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;
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(IPublishedRequest frequest)
2018-06-29 19:52:40 +02:00
{
if (frequest.UmbracoContext != null && frequest.UmbracoContext.InPreviewMode == false
&& _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);
node = frequest.UmbracoContext.Content.GetById(nodeId);
2018-06-29 19:52:40 +02:00
if (node != null)
{
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
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.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;
}
}
}