private static void AddDynamicControllerPathTransformations(Controller controller, string virtualPath, string currentPackage, List <Func <string, string> > pathTransformations) { if (controller != null && controller.Request != null && controller.Request.QueryString != null && controller.RouteData != null && controller.RouteData.Values.ContainsKey("widgetName") && (string)controller.RouteData.Values["widgetName"] == "DynamicContent") { var controlId = controller.Request.QueryString["controlId"] as string; Guid controlIdGuid; if (!string.IsNullOrEmpty(controlId) && Guid.TryParse(controlId, out controlIdGuid)) { var controlObjectData = PageManager.GetManager().GetControl <ObjectData>(controlIdGuid); if (controlObjectData != null && controlObjectData.Properties != null) { var controllerWidgetProperty = controlObjectData.Properties.FirstOrDefault(x => x.Name == "WidgetName"); if (controllerWidgetProperty != null) { var dynamicControllerWidgetName = controllerWidgetProperty.Value; if (!string.IsNullOrEmpty(dynamicControllerWidgetName)) { pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(virtualPath, currentPackage, dynamicControllerWidgetName)); } } } } } }
private static IList <Func <string, string> > GetControllerPathTransformations(Controller controller, string customPath) { var packagesManager = new PackageManager(); var currentPackage = packagesManager.GetCurrentPackage(); var pathTransformations = new List <Func <string, string> >(); if (controller.RouteData != null && controller.RouteData.Values.ContainsKey("widgetName")) { var widgetName = (string)controller.RouteData.Values["widgetName"]; var controllerType = FrontendManager.ControllerFactory.ResolveControllerType(widgetName); var widgetVp = AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType)); pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(widgetVp, currentPackage, widgetName)); } var controllerVp = customPath ?? AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controller.GetType().Assembly)); pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(controllerVp, currentPackage)); var frontendVp = AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(typeof(FrontendControllerFactory).Assembly)); if (!string.Equals(controllerVp, frontendVp, StringComparison.OrdinalIgnoreCase)) { pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(frontendVp, currentPackage)); } return(pathTransformations); }
private static EnhanceViewEnginesAttribute GetEnhanceAttribute(Type controllerType) { var enhanceAttr = controllerType.GetCustomAttributes(typeof(EnhanceViewEnginesAttribute), true).FirstOrDefault() as EnhanceViewEnginesAttribute; if (enhanceAttr != null) { return(enhanceAttr); } var key = controllerType.FullName; if (!FrontendControllerFactory.EnhanceAttributes.ContainsKey(key)) { lock (FrontendControllerFactory.EnhanceAttributes) { if (!FrontendControllerFactory.EnhanceAttributes.ContainsKey(key)) { var newEnhanceAttr = new EnhanceViewEnginesAttribute { Disabled = !FrontendControllerFactory.IsInDefaultMvcNamespace(controllerType), VirtualPath = AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType.Assembly)) }; FrontendControllerFactory.EnhanceAttributes.Add(key, newEnhanceAttr); } } } enhanceAttr = FrontendControllerFactory.EnhanceAttributes[key]; return(enhanceAttr); }
public void CreateController_DummyController_NewControllerViewEnginesHaveAdditinalSearchPaths() { //Arrange var controllerFactory = new FrontendControllerFactory(); controllerFactory.RegisterController(typeof(DummyController).Name, typeof(DummyController)); var viewEngine = new RazorViewEngine(); ViewEngines.Engines.Add(viewEngine); try { //Act var controller = (Controller)controllerFactory.CreateController(new DummyHttpContext().Request.RequestContext, "Dummy"); //Assert var controllerVe = controller.ViewEngineCollection.OfType<RazorViewEngine>().FirstOrDefault(); Assert.IsNotNull(controllerVe, "The newly created controller does not have the expected view engine."); var containerVp = "~/" + FrontendManager.VirtualPathBuilder.GetVirtualPath(typeof(DummyController).Assembly); Assert.IsTrue(controllerVe.ViewLocationFormats.Any(v => v.StartsWith(containerVp)), "The newly created controller does not have its container path in the view locations."); } finally { ViewEngines.Engines.Remove(viewEngine); } }
public void CreateController_DecoratedWithViewEnhanceViewEnginesAttribute_NewControllersViewEnginesHaveCustomTransformations() { //Arrange var controllerFactory = new FrontendControllerFactory(); controllerFactory.RegisterController(typeof(DummyEnhancedController).Name, typeof(DummyEnhancedController)); var viewEngine = new RazorViewEngine(); ViewEngines.Engines.Add(viewEngine); try { //Act var controller = (Controller)controllerFactory.CreateController(new DummyHttpContext().Request.RequestContext, "DummyEnhanced"); //Assert var controllerVe = controller.ViewEngineCollection.OfType<RazorViewEngine>().FirstOrDefault(); Assert.IsNotNull(controllerVe, "The newly created controller does not have the expected view engine."); Assert.IsTrue(controllerVe.ViewLocationFormats.Any(p => p.StartsWith("~/" + DummyEnhancedController.CustomControllerPath)), "The newly created controller does not have its custom path in the view locations."); } finally { ViewEngines.Engines.Remove(viewEngine); } }
/// <summary> /// Enhances the view engines. /// </summary> /// <param name="controller">The controller.</param> internal static void EnhanceViewEngines(Controller controller) { var enhanceAttr = FrontendControllerFactory.GetEnhanceAttribute(controller.GetType()); if (!enhanceAttr.Disabled) { controller.UpdateViewEnginesCollection(() => FrontendControllerFactory.GetControllerPathTransformations(controller, enhanceAttr.VirtualPath)); } }
private static void AddDynamicControllerPathTransformations(Controller controller, string virtualPath, string currentPackage, List <Func <string, string> > pathTransformations) { var dynamicControllerWidgetName = controller.ResolveDynamicControllerWidgetName(); if (string.IsNullOrEmpty(dynamicControllerWidgetName)) { return; } pathTransformations.Add(FrontendControllerFactory.GetPathTransformation(virtualPath, currentPackage, dynamicControllerWidgetName)); }
/// <summary> /// Creates the specified controller by using the specified request context. /// </summary> /// <returns>The controller.</returns> /// <param name="requestContext"> /// The context of the HTTP request, which includes the HTTP context and route data. /// </param> /// <param name="controllerName">The name of the controller.</param> /// <exception cref="T:System.ArgumentNullException"> /// The <paramref name="requestContext"/> parameter is null. /// </exception> /// <exception cref="T:System.ArgumentException"> /// The <paramref name="controllerName"/> parameter is null or empty. /// </exception> public override IController CreateController(RequestContext requestContext, string controllerName) { var baseController = base.CreateController(requestContext, controllerName); var controller = baseController as Controller; if (controller != null) { FrontendControllerFactory.EnhanceViewEngines(controller); } return baseController; }
private EnhanceViewEnginesAttribute GetEnhanceAttribute(Type controllerType) { EnhanceViewEnginesAttribute enhanceAttr = controllerType.GetCustomAttributes(typeof(EnhanceViewEnginesAttribute), true).FirstOrDefault() as EnhanceViewEnginesAttribute; if (enhanceAttr != null) { return(enhanceAttr); } else { enhanceAttr = new EnhanceViewEnginesAttribute(); enhanceAttr.Disabled = !this.IsInDefaultMvcNamespace(controllerType); enhanceAttr.VirtualPath = FrontendControllerFactory.AppendDefaultPath(FrontendManager.VirtualPathBuilder.GetVirtualPath(controllerType.Assembly)); return(enhanceAttr); } }