2

是否可以仅将 viewstart 用于特定的控制器和视图?我只在视图文件夹中使用 _Layou.cshtml 文件。现在我将 _ViewStart.cshtml 添加为视图文件夹内的公共视图,并将 _Layout 移动到共享文件夹。

这是程序结构:

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

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

_Layout.cshtml
{
//design code for Index.chtml
}

根据上面的代码, _Layout 为 homecontroller 呈现。完成第一行提到的更改后,我将在我使用的每个控制器中的 _Layout.cshtml 中获取控件。我使用了近 6 个控制器。如何在不影响整个代码的情况下进行此更改。请帮忙。

PS:我需要在程序中引入 _ViewStart,因为我正在将 openid 与我已经开发的项目集成。

4

2 回答 2

2
  1. 您可以创建另一个_ViewStart.cshtml(例如在Views/[controller]子文件夹中)将覆盖根目录,例如:

    @{ Layout = null; }

  2. 您可以简单地使用ViewBag来确定是否使用布局:

    public ActionResult AnotherAction()
    {
         ....
         ViewBag.NoLayout = true;
    
         return View();
    }
    

    在你的_ViewStart

    @{
    if (ViewBag.NoLayout == null || !ViewBag.NoLayout)
         Layout = "~/Views/_Layout.cshtml";
    }
    
于 2013-06-06T07:23:04.537 回答
1

您可以MVC3 Razor layouts在 Scott Guthrie 的博客上了解更多信息

于 2013-06-05T13:58:33.260 回答