Files
Umbraco-CMS/src/Umbraco.Web/Mvc/PluginControllerArea.cs
Shannon Deminick 5ece0b5af0 Surface controllers now auto-routing, both locally declared and plugin types (which require a [PluginController] attribute and an Area Name assigned).
BeginUmbracoForm working for both local and plugin SurfaceControllers. SurfaceController class now inherits from PluginController which exposes all things a dev would
need including making IPublishedContentStore and IPublishedMediaStore public which can be accessed simply by properties on the controller.
Updated web.config for views to include Umbraco.Web namespace for all of our helper methods.
BeginUmbracoForm now has many more overrides for which you can specify a type or a generic type (we no longer require GUIDs :)
2012-09-26 13:42:03 +07:00

69 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using System.Web.Routing;
using Umbraco.Core;
using Umbraco.Core.Configuration;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// A custom area for controllers that are plugins
/// </summary>
internal class PluginControllerArea : AreaRegistration
{
private readonly IEnumerable<PluginControllerMetadata> _surfaceControllers;
private readonly string _areaName;
/// <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>
/// <param name="pluginControllers"></param>
public PluginControllerArea(IEnumerable<PluginControllerMetadata> pluginControllers)
{
//TODO: When we have other future plugin controllers we need to combine them all into one list here to do our validation.
var controllers = pluginControllers.ToArray();
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);
}
}
//get the surface controllers
_surfaceControllers = controllers.Where(x => TypeHelper.IsTypeAssignableFrom<SurfaceController>(x.ControllerType));
}
public override void RegisterArea(AreaRegistrationContext context)
{
MapRouteSurfaceControllers(context.Routes, _surfaceControllers);
}
public override string AreaName
{
get { return _areaName; }
}
/// <summary>
/// Registers all surface controller routes
/// </summary>
/// <param name="routes"></param>
/// <param name="surfaceControllers"></param>
private void MapRouteSurfaceControllers(RouteCollection routes, IEnumerable<PluginControllerMetadata> surfaceControllers)
{
foreach (var s in surfaceControllers)
{
this.RouteControllerPlugin(s.ControllerName, s.ControllerType, routes, "Surface", "Index", UrlParameter.Optional, "surface");
}
}
}
}