Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrect way to use _viewstart.cshtml and partial Razor views?
    text
    copied!<p>I'm using <a href="http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx" rel="nofollow noreferrer"><strong>_viewstart.cshtml</strong> to automagically assign the same Razor Layout</a> to my views.</p> <p>It's a dead simple file in the root of my Views folder that looks like this:</p> <pre><code>@{ Layout = "~/Views/Shared/_Layout.cshtml"; } </code></pre> <p>This is more DRY than adding the @Layout directive to every single view.</p> <p>However, this poses a problem for Razor <strong>partial</strong> views, because they run the contents of _viewstart.cshtml and therefore incorrectly assign themselves a layout, which makes them, um, no longer partial. </p> <p>Here's a hypothetical project, showing the _viewstart.cshtml file, the shared _layout.shtml file, and a partial view ("AnonBar.cshtml").</p> <p><img src="https://dl.dropbox.com/u/58785/RazorPartialViewstart.PNG" alt="Example project structure"></p> <p>Currently, the way that I'm getting around this is by adding the following line to every partial view:</p> <pre><code>@{ Layout = ""; } </code></pre> <p>This seems like the wrong way to denote a view as a partial in Razor. (Note that unlike the web forms view engine, the file extension is the same for partial views.)</p> <p>Other options I considered but that are even worse:</p> <ul> <li>Putting all partial views into a common folder, so they could share a common _viewstart.cshtml. This breaks the convention of views being in the same folder as their controller.</li> <li>Not using partial views. </li> </ul> <p>Is this something that is still being fleshed out by the Razor view engine team, or am I missing a fundamental concept?</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