How to make a certain View in MVC not inherit _Layout.cshtml?
Solution 1
Use.
@{
Layout = null
}
In whatever .cshtml
file you don't want to inherit the _layout.cshtml
Solution 2
It inherits Layout because of your _ViewStart.cshtml
file. Look at your ViewStart and delete related line. Then if you want to use Layout in your page you have to specify your Layout in each page.
Mike Marks
My name is Mike and I work for a leading marketing cloud provider, headquartered in Indianapolis, but I work remote in Oklahoma. My experience lies in: ASP.NET C# MVC (3, 4, 5) WebAPI (1, 2) HTML JavaScript/jQuery (not an expert by any means) JSON MSSQL
Updated on June 06, 2022Comments
-
Mike Marks almost 2 years
I'm using ASP.NET MVC5, razor syntax. I need a specific "Create" view to NOT inherit the
_Layout.cshtml
Shared View.Basically, in this particular View, I don't want any of the
_Layout.cshtml
features like the navigation menu, footer, etc. -
Mike Marks over 10 yearsI agree this is an approach, but doing this will require me to add the layout manually to every view I want to inherit the layout.. this would be good if I had maybe 1 or 2 views that needed the layout, but for me, it's the opposite, I only need 1 view to not have the layout, so
Layout = null
seems like the best approach for me. -
Nico almost 8 yearsIn any View page (cshtml) you set it as you would normally set your view