Files
Umbraco-CMS/src/Umbraco.Web/Mvc/ControllerFactoryExtensions.cs

32 lines
1.3 KiB
C#
Raw Normal View History

2018-06-29 19:52:40 +02:00
using System;
using System.Web.Mvc;
using System.Web.Routing;
namespace Umbraco.Web.Mvc
{
internal static class ControllerFactoryExtensions
{
/// <summary>
/// Gets a controller type by the name
/// </summary>
/// <param name="factory"></param>
/// <param name="requestContext"></param>
/// <param name="controllerName"></param>
/// <returns></returns>
/// <remarks>
2018-10-25 17:26:23 +02:00
/// This is related to issue: http://issues.umbraco.org/issue/U4-1726. We already have a method called GetControllerTypeInternal on our MasterControllerFactory,
2018-06-29 19:52:40 +02:00
/// however, we cannot always guarantee that the usage of this will be a MasterControllerFactory like during unit tests. So we needed to create
/// this extension method to do the checks instead.
/// </remarks>
internal static Type GetControllerTypeInternal(this IControllerFactory factory, RequestContext requestContext, string controllerName)
{
2018-10-25 17:26:23 +02:00
if (factory is MasterControllerFactory controllerFactory)
2018-06-29 19:52:40 +02:00
return controllerFactory.GetControllerTypeInternal(requestContext, controllerName);
//we have no choice but to instantiate the controller
var instance = factory.CreateController(requestContext, controllerName);
2018-10-25 17:26:23 +02:00
return instance?.GetType();
2018-06-29 19:52:40 +02:00
}
}
}