ASP.NET MVC EditorTemplate sub folders
Asked Answered
T

2

11

I am working on, what I would consider to be, a large ASP.NET MVC website. Currently there are nearly 100 editor templates (all for 1 controller), and this number will grow.

What I want to accomplish is organize my views to make them easier to find and version. This 'version' step is what will make the views multiply as time goes on. you can think of this project as an Question/Answer application, where Exams are created, and can be pulled up later. Basically, for this particular project, the views/EditorTemplates can't really change once in production, so a new copy must be created for future use. References to the old view would still exist, making that exam look and behave the way it did a year ago. Likewise, new exams will automatically pick up the new version of the view, and use that version. I would like to have this type of structure, but I am up for other ideas.

Views/Shared/EditorTemplates/Common
Views/Shared/EditorTemplates/Common/v2
Views/Shared/EditorTemplates/Common/v3
Views/Shared/EditorTemplates/Department
Views/Shared/EditorTemplates/Department/v2

Note: Even though I will have versioned subdirectories, which implies that I will have multiple versions of the same model and template, the new files will have a unique file name. Also, I am also attempting to use the Razor Generator to compile my views. Not sure if that can be extended to add the additional EditorTemplate search paths or not.

Tatterdemalion answered 21/2, 2014 at 21:21 Comment(3)
You can add your own view engine and customize how and where MVC discovers views.Jibber
I am trying to use the Razor Generator, which compiles the views into a separate assembly. I'm looking for a solution that (hopefully) allow for me to still use that, maybe extend that.Tatterdemalion
I haven't used Razor Generator.Jibber
L
19

The framework won't look there, use local EditorTemplate folders instead, e.g. Views/Department/EditorTemplates.

Editor templates are located by the view engine, which first looks in ~/Views/{1}/{0}.cshtml and then in ~/Views/Shared/{0}.cshtml.

For example, if the controller is Department and the model is a String, the framework asks for EditorTemplates/String, and the view engine looks in ~/Views/Department/EditorTemplates/String.cshtml and ~/Views/Shared/EditorTemplates/String.cshtml.

Letdown answered 22/2, 2014 at 0:15 Comment(6)
What are "local EditorTemplates"? How are the paths registered/defined?Tatterdemalion
I'm not sure that this will work for me. All of the EditorTemplates are under the same controller. The 'Department' example was a way for me to categorize my views, which is what this post is about.Tatterdemalion
This answer is a little unclear; when it goes looking for EditorTemplates/String.cshtml, which location is it using to find that? "~/Views/Shared/{0}.cshtml"? Or something else? I tried adding a subfolder like ~/Views/Shared/MySub/EditorTemplates, and adding the path to search as ~/Views/Shared/MySub/{0}.cshtml does not do the trick, sadly.Minna
@ChrisMoschini EditorTemplates/String.cshtml replaces {0}Letdown
@MaxToro Have you tested that? I did, and it failed. The Partials one works, but the EditorTemplates/DisplayTemplates folders fail to be found using this approach. Something more/different is needed.Minna
@MaxToro Can you point me to the place where the razor engine searches for the default template?Homegrown
S
3

Max's answer is a much more elegant and simple answer. If you don't want to do that AND you want a ton of work, you can write your own ViewEngine.

Suspicious answered 22/2, 2014 at 0:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.