using System; using System.Web; using System.Web.Mvc; using System.Web.Routing; using System.Web.UI; using Umbraco.Core; using Umbraco.Core.Cache; using Umbraco.Core.Logging; using Umbraco.Core.Persistence; using Umbraco.Core.Services; using Umbraco.Web.Composing; using Umbraco.Web.Security; using Umbraco.Web.UI.Pages; namespace Umbraco.Web.UI.Controls { /// /// A base class for all Presentation UserControls to inherit from /// public abstract class UmbracoUserControl : UserControl { private ClientTools _clientTools; private UrlHelper _url; /// /// Default constructor /// /// /// /// protected UmbracoUserControl(UmbracoContext umbracoContext, ServiceContext services, CacheHelper appCache) { if (umbracoContext == null) throw new ArgumentNullException(nameof(umbracoContext)); UmbracoContext = umbracoContext; Umbraco = new UmbracoHelper(umbracoContext, services, appCache); Members = new MembershipHelper(umbracoContext); // fixme inject somehow Logger = Current.Logger; ProfilingLogger = Current.ProfilingLogger; Services = Current.Services; } /// /// Empty constructor, uses Singleton to resolve the UmbracoContext /// protected UmbracoUserControl() : this(Current.UmbracoContext, Current.Services, Current.ApplicationCache) { } // for debugging purposes internal Guid InstanceId { get; } = Guid.NewGuid(); /// /// Gets the Umbraco helper. /// public UmbracoHelper Umbraco { get; } /// /// Gets the membership helper; /// public MembershipHelper Members { get; } /// /// Gets the web security helper. /// public WebSecurity Security => UmbracoContext.Security; /// /// Gets the logger. /// public ILogger Logger { get; } /// /// Gets the ProfilingLogger. /// public ProfilingLogger ProfilingLogger { get; } /// /// Gets the Umbraco context. /// public UmbracoContext UmbracoContext { get; } /// /// Gets the services context. /// public ServiceContext Services { get; } /// /// Gets an instance of ClientTools for access to the pages client API. /// public ClientTools ClientTools { get { var page = Page as BasePage; return _clientTools ?? (_clientTools = page != null ? page.ClientTools : new ClientTools(Page)); } } /// /// Gets a Url helper. /// /// This URL helper is created without any route data and an empty request context. public UrlHelper Url => _url ?? (_url = new UrlHelper(Context.Request.RequestContext)); } }