Changed how surface controllers are routed: because we don't load plugins in via App_Plugins/[PackageName] like we did in v5 which meant that we can autoroute all plugin controllers with their own areas, in v4 we have to do something different.
Instead (because we still want areas), we have an attribute called PluginControllerAttribute which allows a dev to assign an area name as a string. A package developer should always ensure that all of their plugin controllers are assigned to the same area. This also lets us get rid of the GUID id part of the plugin (hopefully we can leave it out, just need to run some tests). Since we have areas, package devs can then put their views into their own folders and not clutter up the local devs ~/Views folder. Perhaps we use App_Plugins/[PackageName] for the views like we did in v5 but need to ask on the mail list. Otherwise it will be the standard MVC: ~/Areas/[AreaName]/Views which will still also clutter up the local devs view folder if they are using Areas.
This commit is contained in:
70
src/Umbraco.Web/Mvc/PluginControllerArea.cs
Normal file
70
src/Umbraco.Web/Mvc/PluginControllerArea.cs
Normal file
@@ -0,0 +1,70 @@
|
||||
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<SurfaceController> _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<PluginController> 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.GetMetadata().AreaName.IsNullOrWhiteSpace()))
|
||||
{
|
||||
throw new InvalidOperationException("Cannot create a PluginControllerArea unless all plugin controllers assigned have a PluginControllerAttribute assigned");
|
||||
}
|
||||
_areaName = controllers.First().GetMetadata().AreaName;
|
||||
foreach(var c in controllers)
|
||||
{
|
||||
if (c.GetMetadata().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.GetMetadata().AreaName);
|
||||
}
|
||||
}
|
||||
|
||||
//get the surface controllers
|
||||
_surfaceControllers = controllers.OfType<SurfaceController>();
|
||||
}
|
||||
|
||||
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<SurfaceController> surfaceControllers)
|
||||
{
|
||||
foreach (var s in surfaceControllers)
|
||||
{
|
||||
var meta = s.GetMetadata();
|
||||
this.RouteControllerPlugin(meta.ControllerName, meta.ControllerType, routes, "Surface", "Index", UrlParameter.Optional, "surface");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user