Disable layout in ASP.NET MVC?

Viewed 84016

In MonoRail you can just CancelLayout() to not render the layout. In ASP.NET MVC, the only way to affect the layout seems to be to pass the layout name into the View() method like View("myview", "mylayout"); only it seems that passing null or an empty string doesn't do what I'd want.

I ended up creating an empty layout that just rendered the content, but that seems silly.

"Not Render the layout" means exactly that. In the web forms view engine they call layouts "master pages". I want to render just my action's view and not surround it with the master page.

10 Answers

In MVC 3, you can remove the master layout code with:

   @{
    Layout = "";    
    }

At the beginning of view add this:

@{
    Layout = null;
}

If you want style sheet to remain, you'll need to add reference to it in that view.

You can create a custom ActionResult that does pretty much anything. The ActionResult controls what is sent back to the client as the response. It would be trivial to create a class that extends ActionResult that does nothing.

One alternative is to actually specify a layout but make that layout empty "_EmptyLayout.cshtml" that contains nothing or just a comment that says it contains nothing so later someone sees it as intended.

Related