From 5784449fceb98fece8ba1a551339b917e5fbb737 Mon Sep 17 00:00:00 2001 From: Bjarke Berg Date: Mon, 10 Feb 2020 07:03:50 +0100 Subject: [PATCH] Started replacing UmbracoContext.HttpContext with injection of IHttpContextAccessor --- src/Umbraco.Web/Install/InstallHelper.cs | 6 +++--- src/Umbraco.Web/Macros/MacroRenderer.cs | 8 +++++--- src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs | 2 +- src/Umbraco.Web/Templates/TemplateRenderer.cs | 13 ++++++++----- 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/Umbraco.Web/Install/InstallHelper.cs b/src/Umbraco.Web/Install/InstallHelper.cs index 54d1475ec6..db13ff1ed7 100644 --- a/src/Umbraco.Web/Install/InstallHelper.cs +++ b/src/Umbraco.Web/Install/InstallHelper.cs @@ -18,18 +18,18 @@ namespace Umbraco.Web.Install { private static HttpClient _httpClient; private readonly DatabaseBuilder _databaseBuilder; - private readonly HttpContextBase _httpContext; + private readonly HttpContext _httpContext; private readonly ILogger _logger; private readonly IGlobalSettings _globalSettings; private readonly IUmbracoVersion _umbracoVersion; private readonly IConnectionStrings _connectionStrings; private InstallationType? _installationType; - public InstallHelper(IUmbracoContextAccessor umbracoContextAccessor, + public InstallHelper(IHttpContextAccessor httpContextAccessor, DatabaseBuilder databaseBuilder, ILogger logger, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IConnectionStrings connectionStrings) { - _httpContext = umbracoContextAccessor.UmbracoContext.HttpContext; + _httpContext = httpContextAccessor.HttpContext; _logger = logger; _globalSettings = globalSettings; _umbracoVersion = umbracoVersion; diff --git a/src/Umbraco.Web/Macros/MacroRenderer.cs b/src/Umbraco.Web/Macros/MacroRenderer.cs index d331dfc65f..b64eb730c2 100755 --- a/src/Umbraco.Web/Macros/MacroRenderer.cs +++ b/src/Umbraco.Web/Macros/MacroRenderer.cs @@ -27,8 +27,9 @@ namespace Umbraco.Web.Macros private readonly AppCaches _appCaches; private readonly IMacroService _macroService; private readonly IUserService _userService; + private readonly IHttpContextAccessor _httpContextAccessor; - public MacroRenderer(IProfilingLogger plogger, IUmbracoContextAccessor umbracoContextAccessor, IContentSection contentSection, ILocalizedTextService textService, AppCaches appCaches, IMacroService macroService, IUserService userService) + public MacroRenderer(IProfilingLogger plogger, IUmbracoContextAccessor umbracoContextAccessor, IContentSection contentSection, ILocalizedTextService textService, AppCaches appCaches, IMacroService macroService, IUserService userService, IHttpContextAccessor httpContextAccessor) { _plogger = plogger ?? throw new ArgumentNullException(nameof(plogger)); _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); @@ -37,6 +38,7 @@ namespace Umbraco.Web.Macros _appCaches = appCaches ?? throw new ArgumentNullException(nameof(appCaches)); _macroService = macroService ?? throw new ArgumentNullException(nameof(macroService)); _userService = userService ?? throw new ArgumentNullException(nameof(userService)); + _httpContextAccessor = httpContextAccessor; } #region MacroContent cache @@ -56,7 +58,7 @@ namespace Umbraco.Web.Macros { object key = 0; - if (_umbracoContextAccessor.UmbracoContext.HttpContext?.User?.Identity?.IsAuthenticated ?? false) + if (_httpContextAccessor.HttpContext?.User?.Identity?.IsAuthenticated ?? false) { //ugh, membershipproviders :( var provider = MembershipProviderExtensions.GetMembersMembershipProvider(); @@ -391,7 +393,7 @@ namespace Umbraco.Web.Macros return attributeValue; } - var context = _umbracoContextAccessor.UmbracoContext.HttpContext; + var context = _httpContextAccessor.HttpContext; foreach (var token in tokens) { diff --git a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs index 6e01f76abe..a8f5452ce1 100644 --- a/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs +++ b/src/Umbraco.Web/Mvc/UmbracoViewPageOfTModel.cs @@ -221,7 +221,7 @@ namespace Umbraco.Web.Mvc markupToInject = string.Format(_umbracoSettingsSection.Content.PreviewBadge, Current.IOHelper.ResolveUrl(_globalSettings.UmbracoPath), - Server.UrlEncode(Current.UmbracoContext.HttpContext.Request.Url?.PathAndQuery), + Server.UrlEncode(HttpContext.Current.Request.Url?.PathAndQuery), Current.UmbracoContext.PublishedRequest.PublishedContent.Id); } else diff --git a/src/Umbraco.Web/Templates/TemplateRenderer.cs b/src/Umbraco.Web/Templates/TemplateRenderer.cs index b13719f6e9..4b0093f910 100644 --- a/src/Umbraco.Web/Templates/TemplateRenderer.cs +++ b/src/Umbraco.Web/Templates/TemplateRenderer.cs @@ -2,6 +2,7 @@ using System; using System.Globalization; using System.IO; using System.Linq; +using System.Web; using System.Web.Mvc; using System.Web.Routing; using Umbraco.Core.Configuration.UmbracoSettings; @@ -28,8 +29,9 @@ namespace Umbraco.Web.Templates private readonly ILocalizationService _languageService; private readonly IWebRoutingSection _webRoutingSection; private readonly IShortStringHelper _shortStringHelper; + private readonly IHttpContextAccessor _httpContextAccessor; - public TemplateRenderer(IUmbracoContextAccessor umbracoContextAccessor, IPublishedRouter publishedRouter, IFileService fileService, ILocalizationService textService, IWebRoutingSection webRoutingSection, IShortStringHelper shortStringHelper) + public TemplateRenderer(IUmbracoContextAccessor umbracoContextAccessor, IPublishedRouter publishedRouter, IFileService fileService, ILocalizationService textService, IWebRoutingSection webRoutingSection, IShortStringHelper shortStringHelper, IHttpContextAccessor httpContextAccessor) { _umbracoContextAccessor = umbracoContextAccessor ?? throw new ArgumentNullException(nameof(umbracoContextAccessor)); _publishedRouter = publishedRouter ?? throw new ArgumentNullException(nameof(publishedRouter)); @@ -37,6 +39,7 @@ namespace Umbraco.Web.Templates _languageService = textService ?? throw new ArgumentNullException(nameof(textService)); _webRoutingSection = webRoutingSection ?? throw new ArgumentNullException(nameof(webRoutingSection)); _shortStringHelper = shortStringHelper ?? throw new ArgumentNullException(nameof(shortStringHelper)); + _httpContextAccessor = httpContextAccessor; } public void Render(int pageId, int? altTemplateId, StringWriter writer) @@ -124,7 +127,7 @@ namespace Umbraco.Web.Templates //var queryString = _umbracoContext.HttpContext.Request.QueryString.AllKeys // .ToDictionary(key => key, key => context.Request.QueryString[key]); - var requestContext = new RequestContext(_umbracoContextAccessor.UmbracoContext.HttpContext, new RouteData() + var requestContext = new RequestContext(new HttpContextWrapper(_httpContextAccessor.HttpContext), new RouteData() { Route = RouteTable.Routes["Umbraco_default"] }); @@ -172,7 +175,7 @@ namespace Umbraco.Web.Templates private void SetNewItemsOnContextObjects(PublishedRequest request) { //now, set the new ones for this page execution - _umbracoContextAccessor.UmbracoContext.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate] = null; + _httpContextAccessor.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate] = null; _umbracoContextAccessor.UmbracoContext.PublishedRequest = request; } @@ -184,7 +187,7 @@ namespace Umbraco.Web.Templates //Many objects require that these legacy items are in the http context items... before we render this template we need to first //save the values in them so that we can re-set them after we render so the rest of the execution works as per normal oldPublishedRequest = _umbracoContextAccessor.UmbracoContext.PublishedRequest; - oldAltTemplate = _umbracoContextAccessor.UmbracoContext.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate]; + oldAltTemplate = _httpContextAccessor.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate]; } /// @@ -193,7 +196,7 @@ namespace Umbraco.Web.Templates private void RestoreItems(PublishedRequest oldPublishedRequest, object oldAltTemplate) { _umbracoContextAccessor.UmbracoContext.PublishedRequest = oldPublishedRequest; - _umbracoContextAccessor.UmbracoContext.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate] = oldAltTemplate; + _httpContextAccessor.HttpContext.Items[Core.Constants.Conventions.Url.AltTemplate] = oldAltTemplate; } } }