Created
June 1, 2023 11:00
-
-
Save skttl/fd1478975cc642071902eaa5c3dc4f98 to your computer and use it in GitHub Desktop.
Umbraco enabled viewcomponent
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Mvc.ViewComponents; | |
using Umbraco.Cms.Core.Cache; | |
using Umbraco.Cms.Core.Logging; | |
using Umbraco.Cms.Core.Models.PublishedContent; | |
using Umbraco.Cms.Core.Services; | |
using Umbraco.Cms.Infrastructure.Persistence; | |
using Umbraco.Cms.Web.Common.Routing; | |
namespace Ecreo.Essentials.ViewComponents; | |
public abstract class UmbracoViewComponent : ViewComponent | |
{ | |
public UmbracoViewComponent( | |
IUmbracoContextAccessor umbracoContextAccessor, | |
IUmbracoDatabaseFactory databaseFactory, | |
ServiceContext services, | |
AppCaches appCaches, | |
IProfilingLogger profilingLogger | |
) | |
{ | |
UmbracoContextAccessor = umbracoContextAccessor; | |
DatabaseFactory = databaseFactory; | |
Services = services; | |
AppCaches = appCaches; | |
ProfilingLogger = profilingLogger; | |
} | |
/// <summary> | |
/// Returns a view located at Partials/Components/{ViewComponentName}.cshtml | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <param name="model"></param> | |
/// <returns></returns> | |
public new ViewViewComponentResult View<TModel>(TModel? model) | |
{ | |
return View(GetViewName(), model); | |
} | |
/// <summary> | |
/// Returns a view located at Partials/Components/{ViewComponentName}.cshtml | |
/// </summary> | |
/// <typeparam name="TModel"></typeparam> | |
/// <param name="model"></param> | |
/// <returns></returns> | |
public new ViewViewComponentResult View() | |
{ | |
return View(GetViewName()); | |
} | |
private string GetViewName() | |
{ | |
var viewName = GetType().Name; | |
viewName = viewName.EndsWith("ViewComponent") ? viewName.Substring(0, viewName.LastIndexOf("ViewComponent")) : viewName; | |
return $"/Views/Partials/Components/{viewName}.cshtml"; | |
} | |
/// <summary> | |
/// Gets the current page. | |
/// </summary> | |
protected virtual IPublishedContent? CurrentPage | |
{ | |
get | |
{ | |
UmbracoRouteValues? umbracoRouteValues = HttpContext.Features.Get<UmbracoRouteValues>(); | |
if (umbracoRouteValues is null) | |
{ | |
throw new InvalidOperationException( | |
$"No {nameof(UmbracoRouteValues)} feature was found in the HttpContext"); | |
} | |
return umbracoRouteValues.PublishedRequest.PublishedContent; | |
} | |
} | |
/// <summary> | |
/// Gets the Umbraco context. | |
/// </summary> | |
public virtual IUmbracoContext UmbracoContext | |
{ | |
get | |
{ | |
IUmbracoContext umbracoContext = UmbracoContextAccessor.GetRequiredUmbracoContext(); | |
return umbracoContext; | |
} | |
} | |
/// <summary> | |
/// Gets the database context accessor. | |
/// </summary> | |
public virtual IUmbracoContextAccessor UmbracoContextAccessor { get; } | |
/// <summary> | |
/// Gets the database context. | |
/// </summary> | |
public IUmbracoDatabaseFactory DatabaseFactory { get; } | |
/// <summary> | |
/// Gets or sets the services context. | |
/// </summary> | |
public ServiceContext Services { get; } | |
/// <summary> | |
/// Gets or sets the application cache. | |
/// </summary> | |
public AppCaches AppCaches { get; } | |
/// <summary> | |
/// Gets or sets the profiling logger. | |
/// </summary> | |
public IProfilingLogger ProfilingLogger { get; } | |
// for debugging purposes | |
internal Guid InstanceId { get; } = Guid.NewGuid(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment