asp.net-mvc 为什么我的视图不包括 _Layout.cshtml?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7767585/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:31:20  来源:igfitidea点击:

Why is my View not including _Layout.cshtml?

asp.net-mvcasp.net-mvc-3asp.net-mvc-2

提问by Kenci

I recently made some changes to my MVC 3 project.

我最近对我的 MVC 3 项目进行了一些更改。

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.

当我运行它时,视图不包含任何文件,如 Site.css。当我调试我的 Index() ActionController 时,它直接跳转到视图,而不包括像 _Layout.cshtml 这样的文件。所以我只得到一个白色背景的视图,没有菜单等。

The Global.asax.cs file contains following code:

Global.asax.cs 文件包含以下代码:

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 = "" }
    );
}

回答by Alex Duggleby

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.

如果您的控制器操作中的断点被击中,则路由可能是错误的,但这不是 _Layout.cshtml 无法加载的原因。

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";
    }
    
  • 您的视图是使用 View() 而不是 PartialView()(后者忽略 ViewStart.cshtml 和 _Layout.cshtml)。
  • 您最近是否移动了 _Layout.cshtml / 您是否重命名了 Shared(或意外创建了 SharedController)?
  • 您的视图是否在顶部包含这样的内容,这会停用 _Layout.cshtml?

    @{
        Layout = "";
    }
    
  • 您的 _ViewStart.cshtml 是否仍然存在,并带有以下激活 _Layout.cshtml 的代码?

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

回答by jsmith

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

将您的“Default2”路线移到“默认”路线上方。

the Default route is more generic so Default2 should be first

默认路由更通用,所以 Default2 应该是第一个

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

此外,在您的视图中确保您指定要使用的布局

@{
    Layout = "yourlayoutpage.cshtml"
}

回答by Jamie Dixon

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

听起来您已经摆脱了索引视图中的布局属性。

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

回答by Computer

I know this has been resolved but for me (MVC 5) I had to add this line of code before the regular view displayed its content

我知道这已经解决了,但对我来说(MVC 5)我必须在常规视图显示其内容之前添加这行代码

public ActionResult Index()
{
    return View();
}