From 800b8b5f7ebf8ac47af3a6bb9b871ee1590b03ba Mon Sep 17 00:00:00 2001 From: Shannon Date: Mon, 13 Apr 2015 15:06:40 +1000 Subject: [PATCH] updates nuspec, creates new UmbracoApiControllerBase which is not auto-routed but contains the base services --- build/NuSpecs/UmbracoCms.Core.nuspec | 2 + src/Umbraco.Web/Umbraco.Web.csproj | 1 + .../WebApi/UmbracoApiController.cs | 115 +--------------- .../WebApi/UmbracoApiControllerBase.cs | 123 ++++++++++++++++++ 4 files changed, 132 insertions(+), 109 deletions(-) create mode 100644 src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs diff --git a/build/NuSpecs/UmbracoCms.Core.nuspec b/build/NuSpecs/UmbracoCms.Core.nuspec index f5a32e7394..35f65fb6a4 100644 --- a/build/NuSpecs/UmbracoCms.Core.nuspec +++ b/build/NuSpecs/UmbracoCms.Core.nuspec @@ -22,6 +22,8 @@ + + diff --git a/src/Umbraco.Web/Umbraco.Web.csproj b/src/Umbraco.Web/Umbraco.Web.csproj index eee8304a1e..ebd35f6f1d 100644 --- a/src/Umbraco.Web/Umbraco.Web.csproj +++ b/src/Umbraco.Web/Umbraco.Web.csproj @@ -894,6 +894,7 @@ + diff --git a/src/Umbraco.Web/WebApi/UmbracoApiController.cs b/src/Umbraco.Web/WebApi/UmbracoApiController.cs index 33df591015..d93c070cf4 100644 --- a/src/Umbraco.Web/WebApi/UmbracoApiController.cs +++ b/src/Umbraco.Web/WebApi/UmbracoApiController.cs @@ -1,127 +1,24 @@ -using System; -using System.ComponentModel; +using System.ComponentModel; using System.Linq; -using System.Web; using System.Web.Http; using System.Web.Http.ModelBinding; -using Microsoft.Owin; -using Umbraco.Core; -using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.Validation; -using Umbraco.Core.Services; using Umbraco.Web.Models.ContentEditing; -using Umbraco.Web.Security; namespace Umbraco.Web.WebApi { - public abstract class UmbracoApiController : ApiController + /// + /// The base class for auto-routed API controllers for Umbraco + /// + public abstract class UmbracoApiController : UmbracoApiControllerBase { protected UmbracoApiController() - : this(UmbracoContext.Current) { - } - protected UmbracoApiController(UmbracoContext umbracoContext) + protected UmbracoApiController(UmbracoContext umbracoContext) : base(umbracoContext) { - if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); - UmbracoContext = umbracoContext; - InstanceId = Guid.NewGuid(); - Umbraco = new UmbracoHelper(umbracoContext); - _membershipHelper = new MembershipHelper(UmbracoContext); } - - private readonly MembershipHelper _membershipHelper; - - /// - /// Tries to retrieve the current HttpContext if one exists. - /// - /// - protected Attempt TryGetHttpContext() - { - return Request.TryGetHttpContext(); - } - - /// - /// Tries to retrieve the current HttpContext if one exists. - /// - /// - protected Attempt TryGetOwinContext() - { - return Request.TryGetOwinContext(); - } - - /// - /// Returns an ILogger - /// - public ILogger Logger - { - get { return ProfilingLogger.Logger; } - } - - /// - /// Returns a ProfilingLogger - /// - public ProfilingLogger ProfilingLogger - { - get { return UmbracoContext.Application.ProfilingLogger; } - } - - /// - /// Returns the current ApplicationContext - /// - public ApplicationContext ApplicationContext - { - get { return UmbracoContext.Application; } - } - - /// - /// Returns a ServiceContext - /// - public ServiceContext Services - { - get { return ApplicationContext.Services; } - } - - /// - /// Returns a DatabaseContext - /// - public DatabaseContext DatabaseContext - { - get { return ApplicationContext.DatabaseContext; } - } - - /// - /// Returns an UmbracoHelper object - /// - public UmbracoHelper Umbraco { get; private set; } - - /// - /// Returns the current UmbracoContext - /// - public UmbracoContext UmbracoContext { get; private set; } - - /// - /// Returns the WebSecurity instance - /// - public WebSecurity Security - { - get { return UmbracoContext.Security; } - } - - /// - /// Returns the MemberHelper instance - /// - public MembershipHelper Members - { - get { return _membershipHelper; } - } - - /// - /// Useful for debugging - /// - internal Guid InstanceId { get; private set; } - } } diff --git a/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs b/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs new file mode 100644 index 0000000000..9df0881aca --- /dev/null +++ b/src/Umbraco.Web/WebApi/UmbracoApiControllerBase.cs @@ -0,0 +1,123 @@ +using System; +using System.Web; +using System.Web.Http; +using Microsoft.Owin; +using Umbraco.Core; +using Umbraco.Core.Logging; +using Umbraco.Core.Services; +using Umbraco.Web.Security; + +namespace Umbraco.Web.WebApi +{ + /// + /// The base class for API controllers that expose Umbraco services - THESE ARE NOT AUTO ROUTED + /// + public abstract class UmbracoApiControllerBase : ApiController + { + protected UmbracoApiControllerBase() + : this(UmbracoContext.Current) + { + + } + + protected UmbracoApiControllerBase(UmbracoContext umbracoContext) + { + if (umbracoContext == null) throw new ArgumentNullException("umbracoContext"); + UmbracoContext = umbracoContext; + InstanceId = Guid.NewGuid(); + Umbraco = new UmbracoHelper(umbracoContext); + _membershipHelper = new MembershipHelper(UmbracoContext); + } + + private readonly MembershipHelper _membershipHelper; + + /// + /// Tries to retrieve the current HttpContext if one exists. + /// + /// + protected Attempt TryGetHttpContext() + { + return Request.TryGetHttpContext(); + } + + /// + /// Tries to retrieve the current HttpContext if one exists. + /// + /// + protected Attempt TryGetOwinContext() + { + return Request.TryGetOwinContext(); + } + + /// + /// Returns an ILogger + /// + public ILogger Logger + { + get { return ProfilingLogger.Logger; } + } + + /// + /// Returns a ProfilingLogger + /// + public ProfilingLogger ProfilingLogger + { + get { return UmbracoContext.Application.ProfilingLogger; } + } + + /// + /// Returns the current ApplicationContext + /// + public ApplicationContext ApplicationContext + { + get { return UmbracoContext.Application; } + } + + /// + /// Returns a ServiceContext + /// + public ServiceContext Services + { + get { return ApplicationContext.Services; } + } + + /// + /// Returns a DatabaseContext + /// + public DatabaseContext DatabaseContext + { + get { return ApplicationContext.DatabaseContext; } + } + + /// + /// Returns an UmbracoHelper object + /// + public UmbracoHelper Umbraco { get; private set; } + + /// + /// Returns the current UmbracoContext + /// + public UmbracoContext UmbracoContext { get; private set; } + + /// + /// Returns the WebSecurity instance + /// + public WebSecurity Security + { + get { return UmbracoContext.Security; } + } + + /// + /// Returns the MemberHelper instance + /// + public MembershipHelper Members + { + get { return _membershipHelper; } + } + + /// + /// Useful for debugging + /// + internal Guid InstanceId { get; private set; } + } +} \ No newline at end of file