2014-11-11 17:52:40 +11:00
|
|
|
using System;
|
|
|
|
|
using System.Collections.Concurrent;
|
2014-03-18 14:28:05 +11:00
|
|
|
using System.Linq;
|
2012-08-07 21:40:34 +06:00
|
|
|
using System.Web.Mvc;
|
2014-07-10 17:28:33 +10:00
|
|
|
using System.Web.Mvc.Async;
|
2012-08-07 21:40:34 +06:00
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Mvc
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that if an action for the Template name is not explicitly defined by a user, that the 'Index' action will execute
|
|
|
|
|
/// </summary>
|
2014-07-10 17:28:33 +10:00
|
|
|
public class RenderActionInvoker : AsyncControllerActionInvoker
|
2012-08-07 21:40:34 +06:00
|
|
|
{
|
|
|
|
|
|
2014-11-11 17:52:40 +11:00
|
|
|
private static readonly ConcurrentDictionary<Type, ReflectedActionDescriptor> IndexDescriptors = new ConcurrentDictionary<Type, ReflectedActionDescriptor>();
|
|
|
|
|
|
2012-08-07 21:40:34 +06:00
|
|
|
/// <summary>
|
|
|
|
|
/// Ensures that if an action for the Template name is not explicitly defined by a user, that the 'Index' action will execute
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="controllerContext"></param>
|
|
|
|
|
/// <param name="controllerDescriptor"></param>
|
|
|
|
|
/// <param name="actionName"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
protected override ActionDescriptor FindAction(ControllerContext controllerContext, ControllerDescriptor controllerDescriptor, string actionName)
|
|
|
|
|
{
|
|
|
|
|
var ad = base.FindAction(controllerContext, controllerDescriptor, actionName);
|
|
|
|
|
|
|
|
|
|
//now we need to check if it exists, if not we need to return the Index by default
|
|
|
|
|
if (ad == null)
|
|
|
|
|
{
|
2013-06-25 09:33:48 +10:00
|
|
|
//check if the controller is an instance of IRenderMvcController
|
|
|
|
|
if (controllerContext.Controller is IRenderMvcController)
|
2012-08-07 21:40:34 +06:00
|
|
|
{
|
2014-11-11 17:52:40 +11:00
|
|
|
return IndexDescriptors.GetOrAdd(controllerContext.Controller.GetType(),
|
|
|
|
|
type => new ReflectedActionDescriptor(
|
|
|
|
|
controllerContext.Controller.GetType().GetMethods()
|
|
|
|
|
.First(x => x.Name == "Index" &&
|
|
|
|
|
x.GetCustomAttributes(typeof (NonActionAttribute), false).Any() == false),
|
|
|
|
|
"Index",
|
|
|
|
|
controllerDescriptor));
|
|
|
|
|
|
|
|
|
|
|
2014-03-18 14:28:05 +11:00
|
|
|
|
2012-08-07 21:40:34 +06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ad;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
}
|