2017-07-20 11:21:28 +02:00
using System ;
2012-09-26 11:04:07 +07:00
using System.Collections.Generic ;
using System.Linq ;
using System.Web.Mvc ;
using System.Web.Routing ;
using Umbraco.Core ;
using Umbraco.Core.Configuration ;
2017-05-30 15:56:27 +02:00
using Umbraco.Core.Composing ;
2013-02-26 22:14:32 +06:00
using Umbraco.Web.WebApi ;
2012-09-26 11:04:07 +07:00
namespace Umbraco.Web.Mvc
{
2017-07-20 11:21:28 +02:00
/// <summary>
/// A custom area for controllers that are plugins
/// </summary>
internal class PluginControllerArea : AreaRegistration
{
2018-04-06 13:51:54 +10:00
private readonly IGlobalSettings _globalSettings ;
2017-07-20 11:21:28 +02:00
private readonly IEnumerable < PluginControllerMetadata > _surfaceControllers ;
2013-02-26 03:45:39 +06:00
private readonly IEnumerable < PluginControllerMetadata > _apiControllers ;
2017-07-20 11:21:28 +02:00
private readonly string _areaName ;
2012-09-26 11:04:07 +07:00
2017-07-20 11:21:28 +02:00
/// <summary>
/// The constructor accepts all types of plugin controllers and will verify that ALL of them have the same areaName assigned to them
/// based on their PluginControllerAttribute. If they are not the same an exception will be thrown.
/// </summary>
2018-04-06 13:51:54 +10:00
/// <param name="globalSettings"></param>
2017-07-20 11:21:28 +02:00
/// <param name="pluginControllers"></param>
2018-04-06 13:51:54 +10:00
public PluginControllerArea ( IGlobalSettings globalSettings , IEnumerable < PluginControllerMetadata > pluginControllers )
2017-07-20 11:21:28 +02:00
{
2018-04-06 13:51:54 +10:00
_globalSettings = globalSettings ;
2017-07-20 11:21:28 +02:00
var controllers = pluginControllers . ToArray ( ) ;
2012-09-26 11:04:07 +07:00
2017-07-20 11:21:28 +02:00
if ( controllers . Any ( x = > x . AreaName . IsNullOrWhiteSpace ( ) ) )
{
throw new InvalidOperationException ( "Cannot create a PluginControllerArea unless all plugin controllers assigned have a PluginControllerAttribute assigned" ) ;
}
_areaName = controllers . First ( ) . AreaName ;
foreach ( var c in controllers )
{
if ( c . AreaName ! = _areaName )
{
throw new InvalidOperationException ( "Cannot create a PluginControllerArea unless all plugin controllers assigned have the same AreaName. The first AreaName found was " + _areaName + " however, the controller of type " + c . GetType ( ) . FullName + " has an AreaName of " + c . AreaName ) ;
}
}
2012-09-26 11:04:07 +07:00
2017-07-20 11:21:28 +02:00
//get the controllers
_surfaceControllers = controllers . Where ( x = > TypeHelper . IsTypeAssignableFrom < SurfaceController > ( x . ControllerType ) ) ;
2013-02-26 03:45:39 +06:00
_apiControllers = controllers . Where ( x = > TypeHelper . IsTypeAssignableFrom < UmbracoApiController > ( x . ControllerType ) ) ;
2017-07-20 11:21:28 +02:00
}
2012-09-26 11:04:07 +07:00
2017-07-20 11:21:28 +02:00
public override void RegisterArea ( AreaRegistrationContext context )
{
MapRouteSurfaceControllers ( context . Routes , _surfaceControllers ) ;
MapRouteApiControllers ( context . Routes , _apiControllers ) ;
}
2012-09-26 11:04:07 +07:00
2017-07-20 11:21:28 +02:00
public override string AreaName
{
get { return _areaName ; }
}
2012-09-26 11:04:07 +07:00
2018-04-06 13:51:54 +10:00
/// <summary>
/// Registers all surface controller routes
/// </summary>
2017-07-20 11:21:28 +02:00
/// <param name="routes"></param>
2018-04-06 13:51:54 +10:00
/// <param name="surfaceControllers"></param>
/// <remarks>
/// The routes will be:
///
/// /Umbraco/[AreaName]/[ControllerName]/[Action]/[Id]
/// </remarks>
2017-07-20 11:21:28 +02:00
private void MapRouteSurfaceControllers ( RouteCollection routes , IEnumerable < PluginControllerMetadata > surfaceControllers )
{
foreach ( var s in surfaceControllers )
{
2018-04-06 13:51:54 +10:00
var route = this . RouteControllerPlugin ( _globalSettings , s . ControllerName , s . ControllerType , routes , "" , "Index" , UrlParameter . Optional , "surface" ) ;
2013-02-19 07:30:46 +06:00
//set the route handler to our SurfaceRouteHandler
route . RouteHandler = new SurfaceRouteHandler ( ) ;
2017-07-20 11:21:28 +02:00
}
}
2013-02-26 03:45:39 +06:00
/// <summary>
/// Registers all api controller routes
/// </summary>
/// <param name="routes"></param>
/// <param name="apiControllers"></param>
private void MapRouteApiControllers ( RouteCollection routes , IEnumerable < PluginControllerMetadata > apiControllers )
{
foreach ( var s in apiControllers )
{
2018-04-06 13:51:54 +10:00
this . RouteControllerPlugin ( _globalSettings , s . ControllerName , s . ControllerType , routes , "" , "" , UrlParameter . Optional , "api" ,
2017-07-20 11:21:28 +02:00
isMvc : false ,
2014-01-15 13:29:17 +11:00
areaPathPrefix : s . IsBackOffice ? "backoffice" : null ) ;
2013-02-26 03:45:39 +06:00
}
}
2017-07-20 11:21:28 +02:00
}
}