Razor View Without Layout

asp.net Mvcasp.net Mvc-3LayoutRazor

asp.net Mvc Problem Overview


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>

asp.net Mvc Solutions


Solution 1 - asp.net Mvc

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 - asp.net Mvc

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 - asp.net Mvc

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 - asp.net Mvc

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).

Solution 5 - asp.net Mvc

Do you have a _ViewStart.cshtml in this directory? I had the same problem you're having when I tried using _ViewStart. Then I renamed it _mydefaultview, moved it to the Views/Shared directory, and switched to specifying no view in cshtml files where I don't want it, and specifying _mydefaultview for the rest. Don't know why this was necessary, but it worked.

Solution 6 - asp.net Mvc

Use:

@{
    Layout = null;
 }

to get rid of the layout specified in _ViewStart.

Solution 7 - asp.net Mvc

I wanted to display the login page without the layout and this works pretty good for me.(this is the _ViewStart.cshtml file) You need to set the ViewBag.Title in the Controller.

@{
    if (! (ViewContext.ViewBag.Title == "Login"))
    {
        Layout = "~/Views/Shared/_Layout.cshtml";        
    } 
}

I know it's a little bit late but I hope this helps some body.

Solution 8 - asp.net Mvc

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.

 @{
 var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
 string cLayout = "";
 if (controller == "Webmaster") {
 cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";
 }
 else {
 cLayout = "~/Views/Shared/_Layout.cshtml";
 }
 Layout = cLayout;
 }

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.

public ActionResult Index()
{
 SampleModel model = new SampleModel();
 //Any Logic
 return View("Index", "_WebmasterLayout", model);
}

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.

@{
   Layout = "~/Views/Shared/_WebmasterLayout.cshtml";
}

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:

@{
  Layout = "~/Views/Shared/_WebmasterLayout.cshtml";
}

Solution 9 - asp.net Mvc

Just create the view as a partial view so that no layout file is used.

Solution 10 - asp.net Mvc

@{ viewbag.title="index" Layout = null; }

Solution 11 - asp.net Mvc

If you are working with apps, try cleaning solution. Fixed for me.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser156888View Question on Stackoverflow
Solution 1 - asp.net MvcJean-Baptiste HENRYView Answer on Stackoverflow
Solution 2 - asp.net MvcSLaksView Answer on Stackoverflow
Solution 3 - asp.net MvcsaviilesView Answer on Stackoverflow
Solution 4 - asp.net MvcErik PhilipsView Answer on Stackoverflow
Solution 5 - asp.net MvcDMulliganView Answer on Stackoverflow
Solution 6 - asp.net MvcSoroushView Answer on Stackoverflow
Solution 7 - asp.net MvcSouhaib LamineView Answer on Stackoverflow
Solution 8 - asp.net MvcAnand GaikwadView Answer on Stackoverflow
Solution 9 - asp.net MvcnsdivView Answer on Stackoverflow
Solution 10 - asp.net Mvcata abedView Answer on Stackoverflow
Solution 11 - asp.net MvcLiam FieldingView Answer on Stackoverflow