Created
December 16, 2012 21:04
-
-
Save jtbennett/4313040 to your computer and use it in GitHub Desktop.
Proposed naming convention for MVC areas installed by NuGet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MvcApplication : System.Web.HttpApplication | |
{ | |
protected void Application_Start() | |
{ | |
... | |
ViewEngines.Engines.Clear(); | |
ViewEngines.Engines.Add(this.GetRazorViewEngine()); | |
} | |
private RazorViewEngine GetRazorViewEngine() | |
{ | |
var razor = new RazorViewEngine(); | |
razor.AreaMasterLocationFormats = AppendInstalledAreaPaths(razor.AreaMasterLocationFormats); | |
razor.AreaPartialViewLocationFormats = AppendInstalledAreaPaths(razor.AreaPartialViewLocationFormats); | |
razor.AreaViewLocationFormats = AppendInstalledAreaPaths(razor.AreaViewLocationFormats); | |
return razor; | |
} | |
private string[] AppendInstalledAreaPaths(string[] original) | |
{ | |
var newPaths = original.Select(path => path.Replace("~/Areas", "~/InstalledAreas")); | |
return original.Concat(newPaths).ToArray(); | |
} | |
} |
This would probably make a good NuGet package that your installed area could depend on. The code could utilize WebActivator to wire up this behavior in the razor view engine, perhaps something like this?
using System.Linq;
using System.Web;
using System.Web.Mvc;
[assembly: PreApplicationStartMethod(typeof(MvcApplication1.App_Start.ViewConfig), "Start")]
namespace MvcApplication1.App_Start
{
public class ViewConfig
{
public static void Start()
{
var razorViewEngine = ViewEngines.Engines.OfType<RazorViewEngine>().FirstOrDefault();
if (razorViewEngine == null)
{
return;
}
razorViewEngine.AreaMasterLocationFormats = AppendInstalledAreaPaths(razorViewEngine.AreaMasterLocationFormats);
razorViewEngine.AreaPartialViewLocationFormats = AppendInstalledAreaPaths(razorViewEngine.AreaPartialViewLocationFormats);
razorViewEngine.AreaViewLocationFormats = AppendInstalledAreaPaths(razorViewEngine.AreaViewLocationFormats);
}
private static string[] AppendInstalledAreaPaths(string[] original)
{
var newPaths = original.Select(path => path.Replace("~/Areas", "~/InstalledAreas"));
return original.Concat(newPaths).ToArray();
}
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The code above configures the RazorViewEngine to first look for views in the traditional Areas folder, then in InstalledAreas. If NuGet packages that install MVC areas installed them to a folder named InstalledAreas, projects could easily customize views. They could then update the packages without worrying about overriding their customizations.
Yes, you can use source control to achieve the same end, but I like the nice separation between "things that came with the package" and "things I changed".
Thoughts?