Get rid of some more duplicate code
This commit is contained in:
@@ -71,10 +71,7 @@ namespace Umbraco.Cms.Web.BackOffice.Controllers
|
||||
var availableLanguages = _localizationService.GetAllLanguages();
|
||||
if (id.HasValue)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
var content = umbracoContext.Content.GetById(true, id.Value);
|
||||
if (content is null)
|
||||
return NotFound();
|
||||
|
||||
@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Routing;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Cms.Web.Common.Controllers;
|
||||
using Umbraco.Cms.Web.Common.Routing;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Web.Common.Filters
|
||||
{
|
||||
@@ -55,10 +56,9 @@ namespace Umbraco.Cms.Web.Common.Filters
|
||||
{
|
||||
IUmbracoContextAccessor umbracoContextAccessor = context.HttpContext.RequestServices.GetRequiredService<IUmbracoContextAccessor>();
|
||||
IPublishedRouter router = context.HttpContext.RequestServices.GetRequiredService<IPublishedRouter>();
|
||||
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
|
||||
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
|
||||
IPublishedRequestBuilder requestBuilder = await router.CreateRequestAsync(umbracoContext.CleanedUmbracoUrl);
|
||||
requestBuilder.SetPublishedContent(content);
|
||||
IPublishedRequest publishedRequest = requestBuilder.Build();
|
||||
|
||||
@@ -155,10 +155,8 @@ namespace Umbraco.Cms.Web.Common.Macros
|
||||
// stores macro content into the cache
|
||||
private async Task AddMacroContentToCacheAsync(MacroModel model, MacroContent macroContent)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
|
||||
// only if cache is enabled
|
||||
if (umbracoContext.InPreviewMode || model.CacheDuration <= 0)
|
||||
return;
|
||||
|
||||
@@ -188,10 +188,8 @@ namespace Umbraco.Cms.Web.Common.Templates
|
||||
|
||||
private void SetNewItemsOnContextObjects(IPublishedRequest request)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
|
||||
// now, set the new ones for this page execution
|
||||
umbracoContext.PublishedRequest = request;
|
||||
}
|
||||
@@ -201,10 +199,7 @@ namespace Umbraco.Cms.Web.Common.Templates
|
||||
/// </summary>
|
||||
private void SaveExistingItems(out IPublishedRequest oldPublishedRequest)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
// 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 = umbracoContext.PublishedRequest;
|
||||
@@ -215,10 +210,7 @@ namespace Umbraco.Cms.Web.Common.Templates
|
||||
/// </summary>
|
||||
private void RestoreItems(IPublishedRequest oldPublishedRequest)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
umbracoContext.PublishedRequest = oldPublishedRequest;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ using Umbraco.Cms.Core.Configuration.Models;
|
||||
using Umbraco.Cms.Core.IO;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Cms.Web.Website.Models;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Web.Website.Controllers
|
||||
{
|
||||
@@ -23,10 +24,7 @@ namespace Umbraco.Cms.Web.Website.Controllers
|
||||
|
||||
public ActionResult Index()
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
var store = umbracoContext.Content;
|
||||
if (store.HasContent())
|
||||
{
|
||||
|
||||
@@ -68,10 +68,8 @@ namespace Umbraco.Extensions
|
||||
/// </remarks>
|
||||
public static IHtmlContent PreviewBadge(this IHtmlHelper helper, IUmbracoContextAccessor umbracoContextAccessor, IHttpContextAccessor httpContextAccessor, GlobalSettings globalSettings, IIOHelper ioHelper, ContentSettings contentSettings)
|
||||
{
|
||||
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
|
||||
if (umbracoContext.InPreviewMode)
|
||||
{
|
||||
var htmlBadge =
|
||||
@@ -672,10 +670,7 @@ namespace Umbraco.Extensions
|
||||
}
|
||||
|
||||
IUmbracoContextAccessor umbracoContextAccessor = GetRequiredService<IUmbracoContextAccessor>(html);
|
||||
if (!umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
var formAction = umbracoContext.OriginalRequestUrl.PathAndQuery;
|
||||
return html.RenderForm(formAction, method, htmlAttributes, controllerName, action, area, additionalRouteVals);
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ using Umbraco.Cms.Core.Services;
|
||||
using Umbraco.Cms.Core.Web;
|
||||
using Umbraco.Cms.Web.Common.Routing;
|
||||
using Umbraco.Cms.Web.Website.Routing;
|
||||
using Umbraco.Extensions;
|
||||
|
||||
namespace Umbraco.Cms.Web.Website.Middleware
|
||||
{
|
||||
@@ -126,10 +127,7 @@ namespace Umbraco.Cms.Web.Website.Middleware
|
||||
{
|
||||
if (pageId != publishedRequest.PublishedContent.Id)
|
||||
{
|
||||
if (!_umbracoContextAccessor.TryGetUmbracoContext(out var umbracoContext))
|
||||
{
|
||||
throw new InvalidOperationException("Wasn't able to get an UmbracoContext");
|
||||
}
|
||||
var umbracoContext = _umbracoContextAccessor.GetRequiredUmbracoContext();
|
||||
IPublishedContent publishedContent = umbracoContext.PublishedSnapshot.Content.GetById(pageId);
|
||||
if (publishedContent == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user