Creates an IAsyncRenderMvcController which can be used instead of IRenderMvcController if developers want access to an async Index action.

This commit is contained in:
Shannon
2015-11-17 17:05:00 +01:00
parent 2998ad8cea
commit 9fd80d791a
6 changed files with 37 additions and 7 deletions

View File

@@ -36,6 +36,7 @@ namespace Umbraco.Web.Mvc
/// <summary>
/// Returns an instance of the default controller instance.
/// </summary>
[Obsolete("This method will be removed in future versions and should not be used to resolve a controller instance, the IControllerFactory is used for that purpose")]
public IRenderMvcController GetControllerInstance()
{
//try the dependency resolver, then the activator
@@ -64,9 +65,10 @@ namespace Umbraco.Web.Mvc
/// <param name="type"></param>
private void ValidateType(Type type)
{
if (TypeHelper.IsTypeAssignableFrom<IRenderMvcController>(type) == false)
if (TypeHelper.IsTypeAssignableFrom<IRenderMvcController>(type) == false
&& TypeHelper.IsTypeAssignableFrom<IAsyncRenderMvcController>(type) == false)
{
throw new InvalidOperationException("The Type specified (" + type + ") is not of type " + typeof(IRenderMvcController));
throw new InvalidOperationException("The Type specified (" + type + ") is not of type " + typeof(IRenderMvcController) + " or of type " + typeof(IAsyncRenderMvcController));
}
}

View File

@@ -0,0 +1,24 @@
using System.Threading.Tasks;
using System.Web.Mvc;
using Umbraco.Web.Models;
namespace Umbraco.Web.Mvc
{
/// <summary>
/// The interface that can be implemented for an async controller to be designated to execute for route hijacking
/// </summary>
public interface IAsyncRenderMvcController : IController
{
/// <summary>
/// The default action to render the front-end view
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <remarks>
/// Ideally we could have made a marker interface as a base interface for both the IRenderMvcController and IAsyncRenderMvcController
/// however that would require breaking changes in other classes like DefaultRenderMvcControllerResolver since the result would have
/// to be changed. Instead we are hiding the underlying interface method.
/// </remarks>
Task<ActionResult> Index(RenderModel model);
}
}

View File

@@ -1,5 +1,6 @@
using System.Web.Http.Filters;
using System.Web.Mvc;
using System.Web.Routing;
using System.Windows.Forms;
using Umbraco.Web.Models;

View File

@@ -26,8 +26,9 @@ namespace Umbraco.Web.Mvc
//now we need to check if it exists, if not we need to return the Index by default
if (ad == null)
{
//check if the controller is an instance of IRenderMvcController
if (controllerContext.Controller is IRenderMvcController)
//check if the controller is an instance of IRenderMvcController or IAsyncRenderMvcController and find the index
if (controllerContext.Controller is IRenderMvcController
|| controllerContext.Controller is IAsyncRenderMvcController)
{
return controllerDescriptor.FindAction(controllerContext, "Index");
}

View File

@@ -310,8 +310,8 @@ namespace Umbraco.Web.Mvc
//check if that controller exists
if (controllerType != null)
{
//ensure the controller is of type 'IRenderMvcController' and ControllerBase
if (TypeHelper.IsTypeAssignableFrom<IRenderMvcController>(controllerType)
//ensure the controller is of type IRenderMvcController/IAsyncRenderMvcController and ControllerBase
if ((TypeHelper.IsTypeAssignableFrom<IRenderMvcController>(controllerType) || TypeHelper.IsTypeAssignableFrom<IAsyncRenderMvcController>(controllerType))
&& TypeHelper.IsTypeAssignableFrom<ControllerBase>(controllerType))
{
//set the controller and name to the custom one
@@ -325,10 +325,11 @@ namespace Umbraco.Web.Mvc
else
{
LogHelper.Warn<RenderRouteHandler>(
"The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Umbraco routing must implement '{2}' and inherit from '{3}'.",
"The current Document Type {0} matches a locally declared controller of type {1}. Custom Controllers for Umbraco routing must implement either '{2}' or '{3}' and inherit from '{4}'.",
() => publishedContentRequest.PublishedContent.DocumentTypeAlias,
() => controllerType.FullName,
() => typeof(IRenderMvcController).FullName,
() => typeof(IAsyncRenderMvcController).FullName,
() => typeof(ControllerBase).FullName);
//we cannot route to this custom controller since it is not of the correct type so we'll continue with the defaults

View File

@@ -304,6 +304,7 @@
<Compile Include="Media\EmbedProviders\Flickr.cs" />
<Compile Include="Models\ContentEditing\SimpleNotificationModel.cs" />
<Compile Include="Models\PublishedContentWithKeyBase.cs" />
<Compile Include="Mvc\IAsyncRenderMvcController.cs" />
<Compile Include="PropertyEditors\DatePreValueEditor.cs" />
<Compile Include="RequestLifespanMessagesFactory.cs" />
<Compile Include="Scheduling\LatchedBackgroundTaskBase.cs" />