Why is my View not including _Layout.cshtml?

25,508

Solution 1

If the breakpoint in your controller action is being hit the routes may be wrong but that's not a reason for _Layout.cshtml to not load.

A few things to check:

  • Is your view using View() and not PartialView() (the latter ignores ViewStart.cshtml and so the _Layout.cshtml).
  • Did you move your _Layout.cshtml recently / Have you renamed Shared (or created a SharedController by accident)?
  • Does your view include something like this at the top which would deactivate the _Layout.cshtml?

    @{
        Layout = "";
    }
    
  • Does your _ViewStart.cshtml still exist with the following code which activates the _Layout.cshtml?

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

Solution 2

Move your "Default2" route up above your "Default" route.

the Default route is more generic so Default2 should be first

also, inside your views make sure that you're specifying the layout to use

@{
    Layout = "yourlayoutpage.cshtml"
}

Solution 3

It sounds like you've got rid of the layout property in your index view.

@{
Layout = "~/Views/Shared/_Layout.cshtml"
}
Share:
25,508

Related videos on Youtube

Kenci
Author by

Kenci

Updated on July 09, 2022

Comments

  • Kenci
    Kenci almost 2 years

    I recently made some changes to my MVC 3 project.

    When I run it, the Views dont include any files like Site.css. When I debug my Index() ActionController, it jumps directly to the View, withouting including files like _Layout.cshtml. So i just get a View with a white background, no menus etc.

    The Global.asax.cs file contains following code:

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
    
        routes.MapRoute(
            "Default2", // Route name
            "{controller}/{action}/{id}/{page}", // URL with parameters
            new { controller = "Survey", action = "DisplayQuestions", id = "", page = "" }
        );
    }
    
  • Kenci
    Kenci over 12 years
    Thank you for the help. It somehow dissapeared :S Works now!
  • Brady Moritz
    Brady Moritz over 10 years
    I still have viewstart, but just today my project stopped loading layout... wth.
  • Brady Moritz
    Brady Moritz over 10 years
    viewStart should take care of this though.
  • Computer
    Computer over 8 years
    why a down vote? At least explain before down voting!!
  • Martin
    Martin about 7 years
    Spot on! My _ViewStart.cshtml was missing.
  • Gabriel Beauchemin-Dauphinais
    Gabriel Beauchemin-Dauphinais over 2 years
    Strange, in a ASP.NET React project, the views and in pages/ folder, and don't need to explicity import the layout, it is done automatically. I thought it would be the same when the views are in /views folder.