Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Danny is correct. The Templates are found the same way that Partial Views are found.</p> <p>By default the WebFormViewEngine and RazorViewEngine are going to search the following locations for a template.</p> <p>For display templates:</p> <blockquote> <p>~/Views/{controller}/DisplayTemplates ~/Views/Shared/DisplayTemplates</p> </blockquote> <p>For editor templates:</p> <blockquote> <p>~/Views/{controller}/EditorTemplates ~/Views/Shared/EditorTemplates</p> </blockquote> <p>I think the name of the sub-directories (i.e., "DisplayTemplates" and "EditorTemplates") are hard-coded into MVC somewhere (I know it's open source and I could find it, but I'm not going to).</p> <p>I think the easiest way to change the location somewhat is to override the ViewEngine. My custom ViewEngine is pretty complicated at this point, but I suspect you could get away with the following.</p> <p>Let's say you want your templates to be in ~/Views/Templates.</p> <p>Create a class that inherits from the view engine you're using now (probably WebFormViewEngine or RazorViewEngine). Add an empty constructor. It should looks like this:</p> <pre><code>namespace MySite { public class MySiteViewEngine : RazorViewEngine // &lt;&lt;-- or WebFormViewEngine { public MySiteViewEngine() { // We'll put some code here in a later step } } } </code></pre> <p>Now, add the following lines to the Application_Start method of Global.asax.cs:</p> <pre><code>ViewEngines.Engines.Clear(); ViewEngines.Engines.Add(new MySiteViewEngine()); </code></pre> <p>At this point, compile and run your application. Everything should be running exactly like it is running now. You're basically using the same view engine you were using before.</p> <p>But now, we want to add a location to search when looking for PartialViews. This is simply done by adding to the <em>PartialViewLocationFormats</em>. So, now in the constructor of your custom view engine, you want to add to the base class' array like so:</p> <pre><code>base.PartialViewLocationFormats = new string[] { "~/Views/Templates/{0}.cshtml" }.Union(base.PartialViewLocationFormats).ToArray&lt;string&gt;(); </code></pre> <p>A couple of notes about the above:</p> <ul> <li>The entry above will make it so that your view engine looks for the String display template at ~/Views/Templates/DisplayTemplates/String.cshtml.</li> <li>The location format in these view engines includes the file extension, so if you're using Razor/C# use "cshtml", Razor/VB use "vbhtml", WebForms add "aspx" and "ascx".</li> <li>The way I'm doing it above, I'm adding my location format to the top of the list but keeping all the default locations. You might consider removing those.</li> <li>Watch the current formats and you'll see that you will also get a controller in the {1} position in the format, so if you wanted to have a Templates directory underneath every controller you could.</li> <li>Careful, once you get started moving things around with a view engine, it gets addictive. You might find yourself moving everything around.</li> </ul> <p>Good luck.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload