Can I Add to the Display/EditorTemplates Search Paths in ASP.NET MVC 3?
Asked Answered
F

3

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

Folkestone answered 7/4, 2011 at 13:22 Comment(1)
#5176524 possible dupe but no answers on there eitherGompers
F
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!

Folkestone answered 7/4, 2011 at 14:42 Comment(1)
As @Simon-Giles said, simply add a location, do not replace the whole engine. Anyway upvoted both.Dormouse
H
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.

Harpy answered 7/4, 2011 at 13:28 Comment(7)
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 goFolkestone
@Anthony Johnston, NuGet packages can be versioned and put under source control as well.Harpy
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 controlFolkestone
@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.Harpy
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 tooFolkestone
@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.Harpy
@Darin I'm just not happy using NuGet in this way, but thanks for the suggestion, I will keep it in mindFolkestone
C
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();
}
Coronel answered 9/9, 2013 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.