Razor View Without Layout
Solution 1:
I think this :
@{
Layout = "";
}
is not the same as this :
@{
Layout = null;
}
I use the second and it's working, no _Viewstart included.
Solution 2:
You (and KMulligan) are misunderstanding _ViewStart
pages.
_ViewStart
will always execute, before your page starts.
It is intended to be used to initialize properties (such as Layout
); it generally should not contain markup. (Since there is no way to override it).
The correct pattern is to make a separate layout page which calls RenderBody
, and set the Layout
property to point to this page in _ViewStart
.
You can then change Layout
in your content pages, and the changes will take effect.
Solution 3:
I think it's better work with individual "views", Im trying to move from PHP to MVC4, its really hard but im on the right way...
Answering your question, if you'll work individual pages, just edit the _ViewStart.cshtml
@{
Layout = null;
}
Another tip if you're getting some issues with CSS path...
Put "../" before of the url
This are the 2 problems that i get today, and I resolve in that way!
Regards;
Solution 4:
Logic for determining if a View should use a layout or not should NOT be in the _viewStart
nor the View
. Setting a default in _viewStart
is fine, but adding any layout logic in the view/viewstart prevents that view from being used anywhere else (with or without layout).
Your Controller Action should:
return PartialView()
By putting this type of logic in the View you breaking the Single responsibility principle rule in M (data), V (visual), C (logic).