问题
Is there a reason why you would want to set the Layout property to null in a _Layout.cshtml?
For example, like this, before rendering the body view?
...
<section id="content">
@{ Layout = null; }
@RenderBody()
</section>
...
It seems pretty nonsensical to me, and removing the line setting Layout doesn't change the way page loads work observationally.
Is there any reason why you would want to set the Layout property in _Layout.cshtml?
回答1:
Layout pages can have a layout too. In nested layouts , you intentionally use Layout property to define super layout of layout page . By default it is null in your layout page if you do not specify any. When you make @{Layout=null}
you are explicitly saying that this your final layout so it is not nested in any super layout.
回答2:
If you want to create a partial view that will not inherit any layout from _Layout than you will use @{ Layout = null; } in the begining of your partial view.
回答3:
I know this is an old thread but my problem solved by this trick! I like to share my solution here
First change
_ViewStart.cshtml
to :@{ if (Layout != "") { Layout = "~/Views/Shared/_Layout.cshtml"; } }
As you can see Layout
is a property and we will check that if it is not equals to ""
Next you have to add this in your views
@{ Layout = ""; }
And you will have happy views without layout!
来源:https://stackoverflow.com/questions/27869398/setting-layout-to-null-in-layout-cshtml