Skip to content

Instantly share code, notes, and snippets.

@jtbennett
Created December 16, 2012 21:04
Show Gist options
  • Save jtbennett/4313040 to your computer and use it in GitHub Desktop.
Save jtbennett/4313040 to your computer and use it in GitHub Desktop.
Proposed naming convention for MVC areas installed by NuGet
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();
}
}
@jtbennett
Copy link
Author

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?

@johncoder
Copy link

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