15

I have a standard set of templates for my mvc projects which I want to keep as an external folder in my source control (SVN)

This means that I cannot put any project specific files in this folder as it will be committed to the wrong place.. .. and my standard templates need to override the ones that are used by MVC itself so they need to be in the place MVC expects overriding templates (e.g. ~/Views/Shared/EditorTemplates)

So where can I put my project specific ones?

Should I put them in ~/Views/Shared/SiteEditorTemplates, for example, and add the path to the search? How would I do that? Or an other suggestions?

thank you, Ant

Anthony Johnston
  • 9,405
  • 4
  • 46
  • 57
  • http://stackoverflow.com/questions/5176024/asp-net-mvc-3-display-templates-and-editor-template-custom-location-how-to possible dupe but no answers on there either – Andrew Apr 07 '11 at 13:25

3 Answers3

20

Ok, got it

The editor code in mvc looks for editors in the PartialViewLocationFormats for the engine adding DisplayTemplates or EditorTemplates to the path.

So, I have created a new path under views ~/Views/Standard/

And plopped my standard stuff in there ~/Views/Standard/EditorTemplates/string.cshtml

Now, register the new path in the engine in global.asax Application_Start

protected void Application_Start() {
    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);

    ViewEngines.Engines.Clear();
    var viewEngine = new RazorViewEngine {
        PartialViewLocationFormats = new[]
        {
            "~/Views/{1}/{0}.cshtml",
            "~/Views/Shared/{0}.cshtml",
            "~/Views/Standard/{0}.cshtml"
        }
    };

    ViewEngines.Engines.Add(viewEngine);
}

Note this will get rid of the webforms view engine and the vb paths, but I don't need them anyway

This allows me to have an external for the ~/Views/Standard in SVN and for the project stuff to override if necessary - rah!

Anthony Johnston
  • 9,405
  • 4
  • 46
  • 57
  • As @Simon-Giles said, simply add a location, do not replace the whole engine. Anyway upvoted both. – kpull1 Jul 29 '15 at 08:28
4

Personally I externalize specific templates as a NuGet package and everytime I start a new ASP.NET MVC project I simply import this NuGet package and it deploys the templates at their respective locations (~/Views/Shared/EditorTemplates) in order to override the default ones.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I really want to have source control over them, so I can version and branch them etc. if I was further down the line with them it would be a good way to go – Anthony Johnston Apr 07 '11 at 13:32
  • @Anthony Johnston, NuGet packages can be versioned and put under source control as well. – Darin Dimitrov Apr 07 '11 at 13:33
  • Yes, but really? I can right click my folder and update or commit to source from VS, with Nuget I would have to copy the files back to a package folder, regenerate the package, upload it to NuGet, wait a mo, go back to my other project and update. Don't take this as a slight on NuGet, I love it, but its not as convenient for code in development as proper source control – Anthony Johnston Apr 07 '11 at 13:39
  • @Anthony Johnston, that's the reason why I've setup a custom build step which does automatically all you described so that I don"t have to do it manually. I simply update the source code and click the build button. The rest is taken care of for me. – Darin Dimitrov Apr 07 '11 at 13:40
  • I could add a build step to move the files from another externals dir to the EditorTemplates dir.. but then changes made while debugging would require a build.. and code in two places allows for problems too – Anthony Johnston Apr 07 '11 at 13:47
  • @Anthony Johnston, no the build step should only publish the NuGet package at some central server. It's client applications that should update their packages respectively. – Darin Dimitrov Apr 07 '11 at 13:49
  • @Darin I'm just not happy using NuGet in this way, but thanks for the suggestion, I will keep it in mind – Anthony Johnston Apr 07 '11 at 14:01
3

Instead of replacing the RazorView engine (as was suggested by Anthony Johnston) you can just alter existing RazorViewEngine's PartialViewLocationFormats property. This code goes in Application_Start:

System.Web.Mvc.RazorViewEngine rve = (RazorViewEngine)ViewEngines.Engines
  .Where(e=>e.GetType()==typeof(RazorViewEngine))
  .FirstOrDefault();

string[] additionalPartialViewLocations = new[] { 
  "~/Views/[YourCustomPathHere]"
};

if(rve!=null)
{
  rve.PartialViewLocationFormats = rve.PartialViewLocationFormats
    .Union( additionalPartialViewLocations )
    .ToArray();
}
Simon Giles
  • 776
  • 9
  • 10