2

我有以下项目结构:

项目结构

_ViewStart.cshtml:

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

_Red.cshtml:

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

_Blue.cshtml 没有明确指定的布局。

现在,如果尝试使用 _Red.cshtml 布局呈现 Index.cshtml,也会分层应用 _Layout.cshtml。所以基本上我们有一个嵌套的模板链_Layout->Red->Our page

但是,如果我尝试使用 _Blue.cshtml 呈现 Index.cshtml,则不会应用 _Layout.cshtml。我期待 _Layout.cshtml 按照约定使用 _viewstart 应用于 _Blue.cshtml。相反,我得到了仅应用了 _Blue.cshtml 模板的 Index.cshtml 页面。

我的假设错了吗?

提前致谢!

更新:

控制器方法:

public ActionResult Index()
{
    ViewBag.Message = "Welcome to ASP.NET MVC!";

    return View("Index");
}

所以,我返回的是 ViewResult,而不是 PartialViewResult ;)

4

1 回答 1

3

是的,你的假设是错误的。如果指定了布局 (_Blue.cshtml),则不会应用 _Viewstart 中的布局。_Viewstart 用于您的默认布局,因此您不必在每个页面上都指定它。

于 2012-04-26T09:02:52.550 回答