I just tried this in specific areas of the project. Therefore, if someone tries this in a solution with several projects, please let us know.
Area support has been added in MVC2. However, the views for your controllers should be in your Views main folder. The solution that I propose here will allow you to keep your specific areas in each area. If your project is structured as shown below with a block being an area.
+ Areas <-- folder + Blog <-- folder + Views <-- folder + Shared <-- folder Index.aspx Create.aspx Edit.aspx + Content + Controllers ... ViewEngine.cs
Add this code to the Application_Start method in Global.asax.cs. It will clear your current view engines and use our new ViewEngine instead.
// Area Aware View Engine ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new AreaViewEngine());
Then create a file called ViewEngine.cs and add the code below.
using System; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Web.Mvc; namespace MyNamespace { public class AreaViewEngine : WebFormViewEngine { public AreaViewEngine() { // {0} = View name // {1} = Controller name // Master Page locations MasterLocationFormats = new[] { "~/Views/{1}/{0}.master" , "~/Views/Shared/{0}.master" }; // View locations ViewLocationFormats = new[] { "~/Views/{1}/{0}.aspx" , "~/Views/{1}/{0}.ascx" , "~/Views/Shared/{0}.aspx" , "~/Views/Shared/{0}.ascx" , "~/Areas/{1}/Views/{0}.aspx" , "~/Areas/{1}/Views/{0}.ascx" , "~/Areas/{1}/Views/Shared/{0}.aspx" , "~/Areas/{1}/Views/Shared/{0}.ascx" }; // Partial view locations PartialViewLocationFormats = ViewLocationFormats; } protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { return new WebFormView(partialPath, null); } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { return new WebFormView(viewPath, masterPath); } } // End Class AreaViewEngine } // End Namespace
This will allow you to find and use the views you created in your areas.
This is one of the possible solutions that allows me to view views in a specified area. Does anyone else have a different, better, advanced solution?
thanks