How come when I have Layout = null;
in my view - it still pulls in the default layout?!
Is there some trick to stop it doing that?
Here is my view without layout:
@{
Layout = "";
}
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
@{Html.RenderAction("Head", "Header");}
</head>
<body>
<div>
Home
</div>
</body>
</html>
Here is the rendered output!!
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
<link href="/Content/Site.css" rel="stylesheet" type="text/css" />
<script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
<script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>
<body>
header
</body>
</html>
</head>
<body>
<div>
Home
</div>
</body>
</html>
Use:
to get rid of the layout specified in _ViewStart.
Procedure 1 : Control Layouts rendering by using _ViewStart file in the root directory of the Views folder
This method is the simplest way for beginners to control Layouts rendering in your ASP.NET MVC application. We can identify the controller and render the Layouts as par controller, to do this we can write our code in _ViewStart file in the root directory of the Views folder. Following is an example shows how it can be done.
Procedure 2 : Set Layout by Returning from ActionResult
One the the great feature of ASP.NET MVC is that, we can override the default layout rendering by returning the layout from the ActionResult. So, this is also a way to render different Layout in your ASP.NET MVC application. Following code sample show how it can be done.
Procedure 3 : View - wise Layout (By defining Layout within each view on the top)
ASP.NET MVC provides us such a great feature & faxibility to override the default layout rendering by defining the layout on the view. To implement this we can write our code in following manner in each View.
Procedure 4 : Placing _ViewStart file in each of the directories
This is a very useful way to set different Layouts for each Controller in your ASP.NET MVC application. If we want to set default Layout for each directories than we can do this by putting _ViewStart file in each of the directories with the required Layout information as shown below:
Just create the view as a partial view so that no layout file is used.