Started replacing UmbracoContext.HttpContext with injection of IHttpContextAccessor

This commit is contained in:
Bjarke Berg
2020-02-10 13:09:26 +01:00
parent aa6a70efba
commit d151503dbf
25 changed files with 329 additions and 50 deletions

View File

@@ -16,12 +16,14 @@ namespace Umbraco.Web.Routing
public class ContentFinderByIdPath : IContentFinder
{
private readonly ILogger _logger;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IWebRoutingSection _webRoutingSection;
public ContentFinderByIdPath(IWebRoutingSection webRoutingSection, ILogger logger)
public ContentFinderByIdPath(IWebRoutingSection webRoutingSection, ILogger logger, IHttpContextAccessor httpContextAccessor)
{
_webRoutingSection = webRoutingSection ?? throw new System.ArgumentNullException(nameof(webRoutingSection));
_logger = logger ?? throw new System.ArgumentNullException(nameof(logger));
_httpContextAccessor = httpContextAccessor;
}
/// <summary>
@@ -55,10 +57,10 @@ namespace Umbraco.Web.Routing
if (node != null)
{
//if we have a node, check if we have a culture in the query string
if (frequest.UmbracoContext.HttpContext.Request.QueryString.ContainsKey("culture"))
if (_httpContextAccessor.HttpContext.Request.QueryString.ContainsKey("culture"))
{
//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(frequest.UmbracoContext.HttpContext.Request.QueryString["culture"]);
frequest.Culture = CultureInfo.GetCultureInfo(_httpContextAccessor.HttpContext.Request.QueryString["culture"]);
}
frequest.PublishedContent = node;

View File

@@ -9,10 +9,17 @@
/// </remarks>
public class ContentFinderByPageIdQuery : IContentFinder
{
private readonly IHttpContextAccessor _httpContextAccessor;
public ContentFinderByPageIdQuery(IHttpContextAccessor httpContextAccessor)
{
_httpContextAccessor = httpContextAccessor;
}
public bool TryFindContent(PublishedRequest frequest)
{
int pageId;
if (int.TryParse(frequest.UmbracoContext.HttpContext.Request["umbPageID"], out pageId))
if (int.TryParse(_httpContextAccessor.HttpContext.Request["umbPageID"], out pageId))
{
var doc = frequest.UmbracoContext.Content.GetById(pageId);

View File

@@ -12,11 +12,219 @@ using Umbraco.Core.Configuration.UmbracoSettings;
namespace Umbraco.Web.Routing
{
public interface IPublishedRequest
{
/// <summary>
/// Gets the UmbracoContext.
/// </summary>
IUmbracoContext UmbracoContext { get; }
/// <summary>
/// Gets or sets the cleaned up Uri used for routing.
/// </summary>
/// <remarks>The cleaned up Uri has no virtual directory, no trailing slash, no .aspx extension, etc.</remarks>
Uri Uri { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the Umbraco Backoffice should ignore a collision for this request.
/// </summary>
bool IgnorePublishedContentCollisions { get; set; }
/// <summary>
/// Gets or sets the requested content.
/// </summary>
/// <remarks>Setting the requested content clears <c>Template</c>.</remarks>
IPublishedContent PublishedContent { get; set; }
/// <summary>
/// Gets the initial requested content.
/// </summary>
/// <remarks>The initial requested content is the content that was found by the finders,
/// before anything such as 404, redirect... took place.</remarks>
IPublishedContent InitialPublishedContent { get; }
/// <summary>
/// Gets value indicating whether the current published content is the initial one.
/// </summary>
bool IsInitialPublishedContent { get; }
/// <summary>
/// Gets or sets a value indicating whether the current published content has been obtained
/// from the initial published content following internal redirections exclusively.
/// </summary>
/// <remarks>Used by PublishedContentRequestEngine.FindTemplate() to figure out whether to
/// apply the internal redirect or not, when content is not the initial content.</remarks>
bool IsInternalRedirectPublishedContent { get; }
/// <summary>
/// Gets a value indicating whether the content request has a content.
/// </summary>
bool HasPublishedContent { get; }
/// <summary>
/// Gets the alias of the template to use to display the requested content.
/// </summary>
string TemplateAlias { get; }
/// <summary>
/// Gets a value indicating whether the content request has a template.
/// </summary>
bool HasTemplate { get; }
/// <summary>
/// Gets or sets the content request's domain.
/// </summary>
/// <remarks>Is a DomainAndUri object ie a standard Domain plus the fully qualified uri. For example,
/// the <c>Domain</c> may contain "example.com" whereas the <c>Uri</c> will be fully qualified eg "http://example.com/".</remarks>
DomainAndUri Domain { get; set; }
/// <summary>
/// Gets a value indicating whether the content request has a domain.
/// </summary>
bool HasDomain { get; }
/// <summary>
/// Gets or sets the content request's culture.
/// </summary>
CultureInfo Culture { get; set; }
/// <summary>
/// Gets or sets a value indicating whether the requested content could not be found.
/// </summary>
/// <remarks>This is set in the <c>PublishedContentRequestBuilder</c> and can also be used in
/// custom content finders or <c>Prepared</c> event handlers, where we want to allow developers
/// to indicate a request is 404 but not to cancel it.</remarks>
bool Is404 { get; set; }
/// <summary>
/// Gets a value indicating whether the content request triggers a redirect (permanent or not).
/// </summary>
bool IsRedirect { get; }
/// <summary>
/// Gets or sets a value indicating whether the redirect is permanent.
/// </summary>
bool IsRedirectPermanent { get; }
/// <summary>
/// Gets or sets the url to redirect to, when the content request triggers a redirect.
/// </summary>
string RedirectUrl { get; }
/// <summary>
/// Gets or sets the content request http response status code.
/// </summary>
/// <remarks>Does not actually set the http response status code, only registers that the response
/// should use the specified code. The code will or will not be used, in due time.</remarks>
int ResponseStatusCode { get; }
/// <summary>
/// Gets or sets the content request http response status description.
/// </summary>
/// <remarks>Does not actually set the http response status description, only registers that the response
/// should use the specified description. The description will or will not be used, in due time.</remarks>
string ResponseStatusDescription { get; }
/// <summary>
/// Gets or sets the <c>System.Web.HttpCacheability</c>
/// </summary>
// Note: we used to set a default value here but that would then be the default
// for ALL requests, we shouldn't overwrite it though if people are using [OutputCache] for example
// see: https://our.umbraco.com/forum/using-umbraco-and-getting-started/79715-output-cache-in-umbraco-752
HttpCacheability Cacheability { get; set; }
/// <summary>
/// Gets or sets a list of Extensions to append to the Response.Cache object.
/// </summary>
List<string> CacheExtensions { get; set; }
/// <summary>
/// Gets or sets a dictionary of Headers to append to the Response object.
/// </summary>
Dictionary<string, string> Headers { get; set; }
/// <summary>
/// Prepares the request.
/// </summary>
void Prepare();
/// <summary>
/// Sets the requested content, following an internal redirect.
/// </summary>
/// <param name="content">The requested content.</param>
/// <remarks>Depending on <c>UmbracoSettings.InternalRedirectPreservesTemplate</c>, will
/// preserve or reset the template, if any.</remarks>
void SetInternalRedirectPublishedContent(IPublishedContent content);
/// <summary>
/// Indicates that the current PublishedContent is the initial one.
/// </summary>
void SetIsInitialPublishedContent();
/// <summary>
/// Tries to set the template to use to display the requested content.
/// </summary>
/// <param name="alias">The alias of the template.</param>
/// <returns>A value indicating whether a valid template with the specified alias was found.</returns>
/// <remarks>
/// <para>Successfully setting the template does refresh <c>RenderingEngine</c>.</para>
/// <para>If setting the template fails, then the previous template (if any) remains in place.</para>
/// </remarks>
bool TrySetTemplate(string alias);
/// <summary>
/// Sets the template to use to display the requested content.
/// </summary>
/// <param name="template">The template.</param>
/// <remarks>Setting the template does refresh <c>RenderingEngine</c>.</remarks>
void SetTemplate(ITemplate template);
/// <summary>
/// Resets the template.
/// </summary>
void ResetTemplate();
/// <summary>
/// Indicates that the content request should trigger a redirect (302).
/// </summary>
/// <param name="url">The url to redirect to.</param>
/// <remarks>Does not actually perform a redirect, only registers that the response should
/// redirect. Redirect will or will not take place in due time.</remarks>
void SetRedirect(string url);
/// <summary>
/// Indicates that the content request should trigger a permanent redirect (301).
/// </summary>
/// <param name="url">The url to redirect to.</param>
/// <remarks>Does not actually perform a redirect, only registers that the response should
/// redirect. Redirect will or will not take place in due time.</remarks>
void SetRedirectPermanent(string url);
/// <summary>
/// Indicates that the content request should trigger a redirect, with a specified status code.
/// </summary>
/// <param name="url">The url to redirect to.</param>
/// <param name="status">The status code (300-308).</param>
/// <remarks>Does not actually perform a redirect, only registers that the response should
/// redirect. Redirect will or will not take place in due time.</remarks>
void SetRedirect(string url, int status);
/// <summary>
/// Sets the http response status code, along with an optional associated description.
/// </summary>
/// <param name="code">The http status code.</param>
/// <param name="description">The description.</param>
/// <remarks>Does not actually set the http response status code and description, only registers that
/// the response should use the specified code and description. The code and description will or will
/// not be used, in due time.</remarks>
void SetResponseStatus(int code, string description = null);
}
/// <summary>
/// Represents a request for one specified Umbraco IPublishedContent to be rendered
/// by one specified template, using one specified Culture and RenderingEngine.
/// </summary>
public class PublishedRequest
public class PublishedRequest : IPublishedRequest
{
private readonly IPublishedRouter _publishedRouter;
private readonly IUmbracoSettingsSection _umbracoSettingsSection;

View File

@@ -29,6 +29,7 @@ namespace Umbraco.Web.Routing
private readonly ILogger _logger;
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
private readonly IUserService _userService;
private readonly IHttpContextAccessor _httpContextAccessor;
/// <summary>
/// Initializes a new instance of the <see cref="PublishedRouter"/> class.
@@ -41,7 +42,8 @@ namespace Umbraco.Web.Routing
ServiceContext services,
IProfilingLogger proflog,
IUmbracoSettingsSection umbracoSettingsSection,
IUserService userService)
IUserService userService,
IHttpContextAccessor httpContextAccessor)
{
_webRoutingSection = webRoutingSection ?? throw new ArgumentNullException(nameof(webRoutingSection));
_contentFinders = contentFinders ?? throw new ArgumentNullException(nameof(contentFinders));
@@ -52,6 +54,7 @@ namespace Umbraco.Web.Routing
_logger = proflog;
_umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection));
_userService = userService ?? throw new ArgumentNullException(nameof(userService));
_httpContextAccessor = httpContextAccessor;
}
/// <inheritdoc />
@@ -651,7 +654,7 @@ namespace Umbraco.Web.Routing
var useAltTemplate = request.IsInitialPublishedContent
|| (_webRoutingSection.InternalRedirectPreservesTemplate && request.IsInternalRedirectPublishedContent);
var altTemplate = useAltTemplate
? request.UmbracoContext.HttpContext.Request[Constants.Conventions.Url.AltTemplate]
? _httpContextAccessor.HttpContext.Request[Constants.Conventions.Url.AltTemplate]
: null;
if (string.IsNullOrWhiteSpace(altTemplate))