diff --git a/src/Umbraco.Web/UmbracoContext.cs b/src/Umbraco.Web/UmbracoContext.cs index 1b75f6b920..4ef696900d 100644 --- a/src/Umbraco.Web/UmbracoContext.cs +++ b/src/Umbraco.Web/UmbracoContext.cs @@ -27,6 +27,7 @@ namespace Umbraco.Web private const string HttpContextItemName = "Umbraco.Web.UmbracoContext"; private static readonly object Locker = new object(); + private bool _replacing; private PreviewContent _previewContent; /// @@ -77,8 +78,12 @@ namespace Umbraco.Web /// internal static UmbracoContext EnsureContext(HttpContextBase httpContext, ApplicationContext applicationContext, bool replaceContext) { - if (UmbracoContext.Current != null && !replaceContext) - return UmbracoContext.Current; + if (UmbracoContext.Current != null) + { + if (!replaceContext) + return UmbracoContext.Current; + UmbracoContext.Current._replacing = true; + } var umbracoContext = new UmbracoContext(httpContext, applicationContext, RoutesCacheResolver.Current.RoutesCache); @@ -163,9 +168,9 @@ namespace Umbraco.Web lock (Locker) { //if running in a real HttpContext, this can only be set once - if (System.Web.HttpContext.Current != null && Current != null) + if (System.Web.HttpContext.Current != null && Current != null && !Current._replacing) { - throw new ApplicationException("The current httpContext can only be set once during a request."); + throw new ApplicationException("The current UmbracoContext can only be set once during a request."); } //if there is an HttpContext, return the item