using System;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.SessionState;
using Umbraco.Core;
using Umbraco.Web.Composing;
namespace Umbraco.Web.Mvc
{
///
/// Abstract filtered controller factory used for all Umbraco controller factory implementations
///
public abstract class UmbracoControllerFactory : IFilteredControllerFactory
{
private readonly OverridenDefaultControllerFactory _innerFactory = new OverridenDefaultControllerFactory();
public abstract bool CanHandle(RequestContext request);
public virtual Type GetControllerType(RequestContext requestContext, string controllerName)
{
return _innerFactory.GetControllerType(requestContext, controllerName);
}
///
/// Creates the specified controller by using the specified request context.
///
///
/// The controller.
///
/// The request context.The name of the controller.
public virtual IController CreateController(RequestContext requestContext, string controllerName)
{
var controllerType = GetControllerType(requestContext, controllerName) ??
_innerFactory.GetControllerType(
requestContext,
ControllerExtensions.GetControllerName(Current.DefaultRenderMvcControllerType));
return _innerFactory.GetControllerInstance(requestContext, controllerType);
}
///
/// Gets the controller's session behavior.
///
///
/// The controller's session behavior.
///
/// The request context.The name of the controller whose session behavior you want to get.
public SessionStateBehavior GetControllerSessionBehavior(RequestContext requestContext, string controllerName)
{
return ((IControllerFactory)_innerFactory).GetControllerSessionBehavior(requestContext, controllerName);
}
///
/// Releases the specified controller.
///
/// The controller.
public virtual void ReleaseController(IController controller)
{
_innerFactory.ReleaseController(controller);
}
///
/// By default, only exposes which throws an exception
/// if the controller is not found. Since we want to try creating a controller, and then fall back to if one isn't found,
/// this nested class changes the visibility of 's internal methods in order to not have to rely on a try-catch.
///
///
internal class OverridenDefaultControllerFactory : ContainerControllerFactory
{
public OverridenDefaultControllerFactory()
: base(Current.Factory)
{ }
public new IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return base.GetControllerInstance(requestContext, controllerType);
}
public new Type GetControllerType(RequestContext requestContext, string controllerName)
{
return controllerName.IsNullOrWhiteSpace()
? null
: base.GetControllerType(requestContext, controllerName);
}
}
}
}