Refactored usages of HttpContext.IsDebuggerEnabled to use the IHostingEnvironment.IsDebugMode

This commit is contained in:
Bjarke Berg
2020-02-13 07:56:50 +01:00
parent 85afde0ea1
commit 215cb60b15
12 changed files with 67 additions and 28 deletions
@@ -48,7 +48,7 @@
redirectUrl = Url.Action("AuthorizeUpgrade", "BackOffice")
});
}
@Html.BareMinimumServerVariablesScript(Url, externalLoginUrl, Model.Features, Model.GlobalSettings, Model.UmbracoVersion, Model.UmbracoSettingsSection, Model.IOHelper, Model.TreeCollection)
@Html.BareMinimumServerVariablesScript(Url, externalLoginUrl, Model.Features, Model.GlobalSettings, Model.UmbracoVersion, Model.UmbracoSettingsSection, Model.IOHelper, Model.TreeCollection, Model.HttpContextAccessor, Model.HostingEnvironment)
<script type="text/javascript">
document.angularReady = function (app) {
@@ -110,7 +110,7 @@
on-login="hideLoginScreen()">
</umb-login>
@Html.BareMinimumServerVariablesScript(Url, Url.Action("ExternalLogin", "BackOffice", new { area = ViewData.GetUmbracoPath() }), Model.Features, Model.GlobalSettings, Model.UmbracoVersion, Model.UmbracoSettingsSection, Model.IOHelper, Model.TreeCollection)
@Html.BareMinimumServerVariablesScript(Url, Url.Action("ExternalLogin", "BackOffice", new { area = ViewData.GetUmbracoPath() }), Model.Features, Model.GlobalSettings, Model.UmbracoVersion, Model.UmbracoSettingsSection, Model.IOHelper, Model.TreeCollection, Model.HttpContextAccessor, Model.HostingEnvironment)
<script>
+4 -1
View File
@@ -3,6 +3,7 @@ using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using Umbraco.Core.Cache;
using Umbraco.Core.Hosting;
namespace Umbraco.Web
{
@@ -16,6 +17,7 @@ namespace Umbraco.Web
/// Outputs and caches a partial view in MVC
/// </summary>
/// <param name="appCaches"></param>
/// <param name="hostingEnvironment"></param>
/// <param name="htmlHelper"></param>
/// <param name="partialViewName"></param>
/// <param name="model"></param>
@@ -25,6 +27,7 @@ namespace Umbraco.Web
/// <returns></returns>
public static IHtmlString CachedPartialView(
this AppCaches appCaches,
IHostingEnvironment hostingEnvironment,
HtmlHelper htmlHelper,
string partialViewName,
object model,
@@ -33,7 +36,7 @@ namespace Umbraco.Web
ViewDataDictionary viewData = null)
{
//disable cached partials in debug mode: http://issues.umbraco.org/issue/U4-5940
if (htmlHelper.ViewContext.HttpContext.IsDebuggingEnabled)
if (hostingEnvironment.IsDebugMode)
{
// just return a normal partial view instead
return htmlHelper.Partial(partialViewName, model, viewData);
@@ -27,6 +27,7 @@ using Constants = Umbraco.Core.Constants;
using JArray = Newtonsoft.Json.Linq.JArray;
using Umbraco.Core.Configuration.Grid;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Web.Trees;
@@ -50,6 +51,8 @@ namespace Umbraco.Web.Editors
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
private readonly IIOHelper _ioHelper;
private readonly TreeCollection _treeCollection;
private readonly IHostingEnvironment _hostingEnvironment;
private readonly IHttpContextAccessor _httpContextAccessor;
public BackOfficeController(
IManifestParser manifestParser,
@@ -65,7 +68,9 @@ namespace Umbraco.Web.Editors
IGridConfig gridConfig,
IUmbracoSettingsSection umbracoSettingsSection,
IIOHelper ioHelper,
TreeCollection treeCollection)
TreeCollection treeCollection,
IHostingEnvironment hostingEnvironment,
IHttpContextAccessor httpContextAccessor)
: base(globalSettings, umbracoContextAccessor, services, appCaches, profilingLogger, umbracoHelper)
{
_manifestParser = manifestParser;
@@ -76,6 +81,8 @@ namespace Umbraco.Web.Editors
_umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection));
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
_treeCollection = treeCollection ?? throw new ArgumentNullException(nameof(treeCollection));
_hostingEnvironment = hostingEnvironment;
_httpContextAccessor = httpContextAccessor;
}
protected BackOfficeSignInManager SignInManager => _signInManager ?? (_signInManager = OwinContext.GetBackOfficeSignInManager());
@@ -91,8 +98,8 @@ namespace Umbraco.Web.Editors
public async Task<ActionResult> Default()
{
return await RenderDefaultOrProcessExternalLoginAsync(
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/Default.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection,_ioHelper, _treeCollection)),
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/Default.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection)));
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/Default.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection,_ioHelper, _treeCollection, _httpContextAccessor, _hostingEnvironment)),
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/Default.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection, _httpContextAccessor, _hostingEnvironment)));
}
[HttpGet]
@@ -175,7 +182,7 @@ namespace Umbraco.Web.Editors
{
return await RenderDefaultOrProcessExternalLoginAsync(
//The default view to render when there is no external login info or errors
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/AuthorizeUpgrade.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection)),
() => View(GlobalSettings.Path.EnsureEndsWith('/') + "Views/AuthorizeUpgrade.cshtml", new BackOfficeModel(_features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection, _httpContextAccessor, _hostingEnvironment)),
//The ActionResult to perform if external login is successful
() => Redirect("/"));
}
@@ -255,7 +262,7 @@ namespace Umbraco.Web.Editors
}
//cache the result if debugging is disabled
var result = HttpContext.IsDebuggingEnabled
var result = _hostingEnvironment.IsDebugMode
? GetAssetList()
: AppCaches.RuntimeCache.GetCacheItem<JArray>(
"Umbraco.Web.Editors.BackOfficeController.GetManifestAssetList",
@@ -282,10 +289,10 @@ namespace Umbraco.Web.Editors
[MinifyJavaScriptResult(Order = 1)]
public JavaScriptResult ServerVariables()
{
var serverVars = new BackOfficeServerVariables(Url, _runtimeState, _features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection);
var serverVars = new BackOfficeServerVariables(Url, _runtimeState, _features, GlobalSettings, _umbracoVersion, _umbracoSettingsSection, _ioHelper, _treeCollection, _httpContextAccessor, _hostingEnvironment);
//cache the result if debugging is disabled
var result = HttpContext.IsDebuggingEnabled
var result = _hostingEnvironment.IsDebugMode
? ServerVariablesParser.Parse(serverVars.GetServerVariables())
: AppCaches.RuntimeCache.GetCacheItem<string>(
typeof(BackOfficeController) + "ServerVariables",
+6 -1
View File
@@ -1,5 +1,6 @@
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Web.Features;
using Umbraco.Web.Trees;
@@ -9,7 +10,7 @@ namespace Umbraco.Web.Editors
public class BackOfficeModel
{
public BackOfficeModel(UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection)
public BackOfficeModel(UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection, IHttpContextAccessor httpContextAccessor, IHostingEnvironment hostingEnvironment)
{
Features = features;
GlobalSettings = globalSettings;
@@ -17,6 +18,8 @@ namespace Umbraco.Web.Editors
UmbracoSettingsSection = umbracoSettingsSection;
IOHelper = ioHelper;
TreeCollection = treeCollection;
HttpContextAccessor = httpContextAccessor;
HostingEnvironment = hostingEnvironment;
}
public UmbracoFeatures Features { get; }
@@ -25,5 +28,7 @@ namespace Umbraco.Web.Editors
public IUmbracoSettingsSection UmbracoSettingsSection { get; }
public IIOHelper IOHelper { get; }
public TreeCollection TreeCollection { get; }
public IHttpContextAccessor HttpContextAccessor { get; }
public IHostingEnvironment HostingEnvironment { get; }
}
}
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Core.Models;
using Umbraco.Web.Features;
@@ -13,8 +14,8 @@ namespace Umbraco.Web.Editors
private readonly UmbracoFeatures _features;
public IEnumerable<ILanguage> Languages { get; }
public BackOfficePreviewModel(UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IEnumerable<ILanguage> languages, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection)
: base(features, globalSettings, umbracoVersion, umbracoSettingsSection, ioHelper, treeCollection)
public BackOfficePreviewModel(UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IEnumerable<ILanguage> languages, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection, IHttpContextAccessor httpContextAccessor, IHostingEnvironment hostingEnvironment)
: base(features, globalSettings, umbracoVersion, umbracoSettingsSection, ioHelper, treeCollection, httpContextAccessor, hostingEnvironment)
{
_features = features;
Languages = languages;
@@ -21,6 +21,7 @@ using Umbraco.Web.PropertyEditors;
using Umbraco.Web.Trees;
using Constants = Umbraco.Core.Constants;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
namespace Umbraco.Web.Editors
@@ -34,25 +35,25 @@ namespace Umbraco.Web.Editors
private readonly IRuntimeState _runtimeState;
private readonly UmbracoFeatures _features;
private readonly IGlobalSettings _globalSettings;
private readonly HttpContextBase _httpContext;
private readonly IOwinContext _owinContext;
private readonly IUmbracoVersion _umbracoVersion;
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
private readonly IIOHelper _ioHelper;
private readonly TreeCollection _treeCollection;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IHostingEnvironment _hostingEnvironment;
internal BackOfficeServerVariables(UrlHelper urlHelper, IRuntimeState runtimeState, UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection)
internal BackOfficeServerVariables(UrlHelper urlHelper, IRuntimeState runtimeState, UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection, IHttpContextAccessor httpContextAccessor, IHostingEnvironment hostingEnvironment)
{
_urlHelper = urlHelper;
_runtimeState = runtimeState;
_features = features;
_globalSettings = globalSettings;
_httpContext = _urlHelper.RequestContext.HttpContext;
_owinContext = _httpContext.GetOwinContext();
_umbracoVersion = umbracoVersion;
_umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection));
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
_treeCollection = treeCollection ?? throw new ArgumentNullException(nameof(treeCollection));
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
}
/// <summary>
@@ -364,7 +365,7 @@ namespace Umbraco.Web.Editors
}
},
{
"isDebuggingEnabled", _httpContext.IsDebuggingEnabled
"isDebuggingEnabled", _hostingEnvironment.IsDebugMode
},
{
"application", GetApplicationState()
@@ -373,7 +374,7 @@ namespace Umbraco.Web.Editors
"externalLogins", new Dictionary<string, object>
{
{
"providers", _owinContext.Authentication.GetExternalAuthenticationTypes()
"providers", _httpContextAccessor.HttpContext.GetOwinContext().Authentication.GetExternalAuthenticationTypes()
.Where(p => p.Properties.ContainsKey("UmbracoBackOffice"))
.Select(p => new
{
@@ -460,7 +461,7 @@ namespace Umbraco.Web.Editors
app.Add("cacheBuster", $"{version}.{_runtimeState.Level}.{ClientDependencySettings.Instance.Version}".GenerateHash());
//useful for dealing with virtual paths on the client side when hosted in virtual directories especially
app.Add("applicationPath", _httpContext.Request.ApplicationPath.EnsureEndsWith('/'));
app.Add("applicationPath", _httpContextAccessor.HttpContext.Request.ApplicationPath.EnsureEndsWith('/'));
//add the server's GMT time offset in minutes
app.Add("serverTimeOffset", Convert.ToInt32(DateTimeOffset.Now.Offset.TotalMinutes));
+9 -2
View File
@@ -6,6 +6,7 @@ using System.Web.UI;
using Umbraco.Core;
using Umbraco.Core.Configuration;
using Umbraco.Core.Configuration.UmbracoSettings;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Core.Services;
using Umbraco.Web.Composing;
@@ -31,6 +32,8 @@ namespace Umbraco.Web.Editors
private readonly IUmbracoSettingsSection _umbracoSettingsSection;
private readonly IIOHelper _ioHelper;
private readonly TreeCollection _treeCollection;
private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IHostingEnvironment _hostingEnvironment;
public PreviewController(
UmbracoFeatures features,
@@ -41,7 +44,9 @@ namespace Umbraco.Web.Editors
IUmbracoVersion umbracoVersion,
IUmbracoSettingsSection umbracoSettingsSection,
IIOHelper ioHelper,
TreeCollection treeCollection)
TreeCollection treeCollection,
IHttpContextAccessor httpContextAccessor,
IHostingEnvironment hostingEnvironment)
{
_features = features;
_globalSettings = globalSettings;
@@ -52,6 +57,8 @@ namespace Umbraco.Web.Editors
_umbracoSettingsSection = umbracoSettingsSection ?? throw new ArgumentNullException(nameof(umbracoSettingsSection));
_ioHelper = ioHelper ?? throw new ArgumentNullException(nameof(ioHelper));
_treeCollection = treeCollection;
_httpContextAccessor = httpContextAccessor;
_hostingEnvironment = hostingEnvironment;
}
[UmbracoAuthorize(redirectToUmbracoLogin: true)]
@@ -60,7 +67,7 @@ namespace Umbraco.Web.Editors
{
var availableLanguages = _localizationService.GetAllLanguages();
var model = new BackOfficePreviewModel(_features, _globalSettings, _umbracoVersion, availableLanguages, _umbracoSettingsSection, _ioHelper, _treeCollection);
var model = new BackOfficePreviewModel(_features, _globalSettings, _umbracoVersion, availableLanguages, _umbracoSettingsSection, _ioHelper, _treeCollection, _httpContextAccessor, _hostingEnvironment);
if (model.PreviewExtendedHeaderView.IsNullOrWhiteSpace() == false)
{
@@ -5,6 +5,7 @@ using System.Web;
using System.Web.Mvc;
using Microsoft.Owin.Security;
using Newtonsoft.Json;
using Umbraco.Core.Hosting;
using Umbraco.Core.IO;
using Umbraco.Web.Composing;
using Umbraco.Web.Editors;
@@ -39,9 +40,9 @@ namespace Umbraco.Web
/// These are the bare minimal server variables that are required for the application to start without being authenticated,
/// we will load the rest of the server vars after the user is authenticated.
/// </remarks>
public static IHtmlString BareMinimumServerVariablesScript(this HtmlHelper html, UrlHelper uri, string externalLoginsUrl, UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection)
public static IHtmlString BareMinimumServerVariablesScript(this HtmlHelper html, UrlHelper uri, string externalLoginsUrl, UmbracoFeatures features, IGlobalSettings globalSettings, IUmbracoVersion umbracoVersion, IUmbracoSettingsSection umbracoSettingsSection, IIOHelper ioHelper, TreeCollection treeCollection, IHttpContextAccessor httpContextAccessor, IHostingEnvironment hostingEnvironment)
{
var serverVars = new BackOfficeServerVariables(uri, Current.RuntimeState, features, globalSettings, umbracoVersion, umbracoSettingsSection, ioHelper, treeCollection);
var serverVars = new BackOfficeServerVariables(uri, Current.RuntimeState, features, globalSettings, umbracoVersion, umbracoSettingsSection, ioHelper, treeCollection, httpContextAccessor, hostingEnvironment);
var minVars = serverVars.BareMinimumServerVariables();
var str = @"<script type=""text/javascript"">
@@ -105,7 +105,7 @@ namespace Umbraco.Web
var contextualKey = contextualKeyBuilder(model, viewData);
cacheKey.AppendFormat("c{0}-", contextualKey);
}
return Current.AppCaches.CachedPartialView(htmlHelper, partialViewName, model, cachedSeconds, cacheKey.ToString(), viewData);
return Current.AppCaches.CachedPartialView(Current.HostingEnvironment, htmlHelper, partialViewName, model, cachedSeconds, cacheKey.ToString(), viewData);
}
public static MvcHtmlString EditorFor<T>(this HtmlHelper htmlHelper, string templateName = "", string htmlFieldName = "", object additionalViewData = null)
@@ -2,6 +2,8 @@
using System.Web.UI;
using ClientDependency.Core;
using ClientDependency.Core.CompositeFiles;
using Umbraco.Composing;
using Umbraco.Core.Hosting;
namespace Umbraco.Web.Mvc
{
@@ -13,6 +15,18 @@ namespace Umbraco.Web.Mvc
/// </remarks>
public class MinifyJavaScriptResultAttribute : ActionFilterAttribute
{
private readonly IHostingEnvironment _hostingEnvironment;
public MinifyJavaScriptResultAttribute()
{
_hostingEnvironment = Current.HostingEnvironment;
}
public MinifyJavaScriptResultAttribute(IHostingEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
/// <summary>
/// Minify the result if in release mode
/// </summary>
@@ -24,7 +38,7 @@ namespace Umbraco.Web.Mvc
if (filterContext.Result == null) return;
var jsResult = filterContext.Result as JavaScriptResult;
if (jsResult == null) return;
if (filterContext.HttpContext.IsDebuggingEnabled) return;
if (_hostingEnvironment.IsDebugMode) return;
//minify the result
var result = jsResult.Script;
+1 -1
View File
@@ -33,7 +33,7 @@ namespace Umbraco.Web.Mvc
/// <summary>
/// Gets or sets the Umbraco context accessor.
/// </summary>
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; set; }
public IUmbracoContextAccessor UmbracoContextAccessor { get; set; }
/// <summary>
/// Gets or sets the services context.