From 45c335702df80547f5e0a66cdbd6f00fb546f243 Mon Sep 17 00:00:00 2001 From: Shannon Deminick Date: Mon, 1 Oct 2012 23:43:47 +0500 Subject: [PATCH] Updated view engine to look only in App_Plugins/ not App_Plugins/Packages/. Updated view engine to automatically create the required razor web.config in the packages Views folder if it is not there already. --- build/Build.proj | 1 - src/Umbraco.Web.UI/Umbraco.Web.UI.csproj | 2 +- src/Umbraco.Web/Mvc/PluginControllerArea.cs | 5 ++ src/Umbraco.Web/Mvc/PluginViewEngine.cs | 64 +++++++++++++++++---- src/Umbraco.Web/Mvc/SurfaceController.cs | 38 ++++++------ 5 files changed, 77 insertions(+), 33 deletions(-) diff --git a/build/Build.proj b/build/Build.proj index 48db7e9b5b..6c33dbdd12 100644 --- a/build/Build.proj +++ b/build/Build.proj @@ -47,7 +47,6 @@ - diff --git a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj index ca8ec15c50..ddce4d4865 100644 --- a/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj +++ b/src/Umbraco.Web.UI/Umbraco.Web.UI.csproj @@ -2221,7 +2221,7 @@ - + diff --git a/src/Umbraco.Web/Mvc/PluginControllerArea.cs b/src/Umbraco.Web/Mvc/PluginControllerArea.cs index 9e221fe667..591d53480a 100644 --- a/src/Umbraco.Web/Mvc/PluginControllerArea.cs +++ b/src/Umbraco.Web/Mvc/PluginControllerArea.cs @@ -58,6 +58,11 @@ namespace Umbraco.Web.Mvc /// /// /// + /// + /// The routes will be: + /// + /// /Umbraco/[AreaName]/[ControllerName]/[Action]/[Id] + /// private void MapRouteSurfaceControllers(RouteCollection routes, IEnumerable surfaceControllers) { foreach (var s in surfaceControllers) diff --git a/src/Umbraco.Web/Mvc/PluginViewEngine.cs b/src/Umbraco.Web/Mvc/PluginViewEngine.cs index 2abc352ef0..bca14c2058 100644 --- a/src/Umbraco.Web/Mvc/PluginViewEngine.cs +++ b/src/Umbraco.Web/Mvc/PluginViewEngine.cs @@ -1,5 +1,7 @@ +using System.IO; using System.Linq; using System.Web.Mvc; +using Umbraco.Core.IO; namespace Umbraco.Web.Mvc { @@ -31,16 +33,16 @@ namespace Umbraco.Web.Mvc var viewLocationsArray = new[] { - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/{1}/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/{1}/{0}.vbhtml") + string.Concat(Constants.PluginsLocation, "/{2}/Views/{1}/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/{1}/{0}.vbhtml") }; //set all of the area view locations to the plugin folder AreaViewLocationFormats = viewLocationsArray .Concat(new[] { - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Shared/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Shared/{0}.vbhtml") + string.Concat(Constants.PluginsLocation, "/{2}/Views/Shared/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/Shared/{0}.vbhtml") }) .ToArray(); @@ -49,17 +51,55 @@ namespace Umbraco.Web.Mvc AreaPartialViewLocationFormats = new[] { //will be used when we have partial view and child action macros - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Partials/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Partials/{0}.vbhtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/MacroPartials/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/MacroPartials/{0}.vbhtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/Partials/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/Partials/{0}.vbhtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/MacroPartials/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/MacroPartials/{0}.vbhtml"), //for partials - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/{1}/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/{1}/{0}.vbhtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Shared/{0}.cshtml"), - string.Concat(Constants.PluginsLocation, "/Packages/{2}/Views/Shared/{0}.vbhtml") + string.Concat(Constants.PluginsLocation, "/{2}/Views/{1}/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/{1}/{0}.vbhtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/Shared/{0}.cshtml"), + string.Concat(Constants.PluginsLocation, "/{2}/Views/Shared/{0}.vbhtml") }; } + + /// + /// Ensures that the correct web.config for razor exists in the /Views folder. + /// + private void EnsureFolderAndWebConfig(ViewEngineResult result) + { + if (result.View == null) return; + var razorResult = result.View as RazorView; + if (razorResult == null) return; + + var folder = Path.GetDirectoryName(IOHelper.MapPath(razorResult.ViewPath)); + //now we need to get the /View/ folder + var viewFolder = folder.Substring(0, folder.LastIndexOf("\\Views\\")) + "\\Views"; + + //ensure the web.config file is in the ~/Views folder + Directory.CreateDirectory(viewFolder); + if (!File.Exists(Path.Combine(viewFolder, "web.config"))) + { + using (var writer = File.CreateText(Path.Combine(viewFolder, "web.config"))) + { + writer.Write(Strings.web_config); + } + } + } + + public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache) + { + var result = base.FindView(controllerContext, viewName, masterName, useCache); + EnsureFolderAndWebConfig(result); + return result; + } + + public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache) + { + var result = base.FindPartialView(controllerContext, partialViewName, useCache); + EnsureFolderAndWebConfig(result); + return result; + } } } \ No newline at end of file diff --git a/src/Umbraco.Web/Mvc/SurfaceController.cs b/src/Umbraco.Web/Mvc/SurfaceController.cs index 0bd4938d0f..340294ad54 100644 --- a/src/Umbraco.Web/Mvc/SurfaceController.cs +++ b/src/Umbraco.Web/Mvc/SurfaceController.cs @@ -9,31 +9,31 @@ namespace Umbraco.Web.Mvc //[PluginController("MyTestSurfaceController")] //public class TestSurfaceController : SurfaceController //{ - // public ActionResult Index() - // { - // return View(); - // //return Content("hello"); - // } + // public ActionResult Index() + // { + // return View(); + // //return Content("hello"); + // } - // public ActionResult PostVals(string name) - // { - // ModelState.AddModelError("name", "bad name!"); - // return CurrentUmbracoPage(); - // } + // public ActionResult PostVals(string name) + // { + // ModelState.AddModelError("name", "bad name!"); + // return CurrentUmbracoPage(); + // } //} //public class LocalSurfaceController : SurfaceController //{ - // public ActionResult Index() - // { - // return View(); - // } + // public ActionResult Index() + // { + // return View(); + // } - // public ActionResult PostVals([Bind(Prefix = "blah")]string name) - // { - // ModelState.AddModelError("name", "you suck!"); - // return this.RedirectToCurrentUmbracoPage(); - // } + // public ActionResult PostVals([Bind(Prefix = "blah")]string name) + // { + // ModelState.AddModelError("name", "you suck!"); + // return this.RedirectToCurrentUmbracoPage(); + // } //} ///