asp.net-mvc 无布局的 Razor 视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6613626/
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:18:58  来源:igfitidea点击:

Razor View Without Layout

asp.net-mvcasp.net-mvc-3layoutrazor

提问by Jean-Baptiste HENRY

How come when I have Layout = null;in my view - it still pulls in the default layout?!

为什么当我Layout = null;在我看来 - 它仍然采用默认布局?!

Is there some trick to stop it doing that?

有什么技巧可以阻止它这样做吗?

Here is my view without layout:

这是我没有布局的观点:

@{
    Layout = "";
}

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
    @{Html.RenderAction("Head", "Header");}
</head>
<body>
    <div>
        Home
    </div>
</body>
</html>

Here is the rendered output!!

这是渲染的输出!!

<!DOCTYPE html>

<html>
<head>
    <title>Index</title>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
    <link href="/Content/Site.css" rel="stylesheet" type="text/css" />
    <script src="/Scripts/jquery-1.5.1.min.js" type="text/javascript"></script>
    <script src="/Scripts/modernizr-1.7.min.js" type="text/javascript"></script>
</head>

<body>
    header
</body>
</html>
</head>
<body>
    <div>
        Home
    </div>
</body>
</html>

采纳答案by DMulligan

Do you have a _ViewStart.cshtmlin this directory? I had the same problem you're having when I tried using _ViewStart. Then I renamed it _mydefaultview, moved it to the Views/Shareddirectory, and switched to specifying no view in cshtml files where I don't want it, and specifying _mydefaultview for the rest. Don't know why this was necessary, but it worked.

_ViewStart.cshtml在这个目录下有吗?我在尝试使用 _ViewStart 时遇到了与您相同的问题。然后我将其重命名为 _mydefaultview,将其移动到Views/Shared目录中,并切换到在我不想要的 cshtml 文件中不指定视图,并为其余部分指定 _mydefaultview。不知道为什么这是必要的,但它有效。

回答by Jean-Baptiste HENRY

I think this :

我认为这 :

@{
    Layout = "";
 }

is not the same as this :

与此不同:

@{
    Layout = null;
 }

I use the second and it's working, no _Viewstart included.

我使用第二个并且它正在工作,不包括 _Viewstart 。

回答by SLaks

You (and KMulligan) are misunderstanding _ViewStartpages.

您(和 KMulligan)是误解_ViewStart页面。

_ViewStartwill alwaysexecute, before your page starts.
It is intended to be used to initialize properties (such as Layout); it generally should not contain markup. (Since there is no way to override it).

_ViewStart始终在您的页面启动之前执行。
它旨在用于初始化属性(例如Layout);它通常不应包含标记。(因为没有办法覆盖它)。

The correct pattern is to make a separate layout page which calls RenderBody, and set the Layoutproperty to point to this page in _ViewStart.

正确的方式是拨打的电话一个单独的页面布局RenderBody,以及设置Layout属性指向此页面_ViewStart

You can then change Layoutin your content pages, and the changes will take effect.

然后您可以Layout在您的内容页面中进行更改,这些更改将生效。

回答by saviiles

I think it's better work with individual "views", Im trying to move from PHP to MVC4, its really hard but im on the right way...

我认为使用个人“视图”更好,我试图从 PHP 迁移到 MVC4,这真的很难,但我走的是正确的道路......

Answering your question, if you'll work individual pages, just edit the _ViewStart.cshtml

回答您的问题,如果您要处理单个页面,只需编辑 _ViewStart.cshtml

@{
  Layout = null;
}

Another tip if you're getting some issues with CSS path...

如果您在 CSS 路径方面遇到一些问题,另一个提示...

Put "../" before of the url

将“../”放在网址之前

This are the 2 problems that i get today, and I resolve in that way!

这是我今天遇到的两个问题,我就是这样解决的!

Regards;

问候;

回答by Erik Philips

Logicfor determining if a View should use a layout or not should NOT be in the _viewStartnor the View. Setting a default in _viewStartis fine, but adding any layout logic in the view/viewstart prevents that view from being used anywhere else (with or without layout).

用于确定视图是否应使用布局的逻辑不应_viewStartView. 设置默认值_viewStart很好,但是在视图/视图开始中添加任何布局逻辑会阻止该视图在其他任何地方使用(有或没有布局)。

Your Controller Action should:

您的控制器操作应该:

return PartialView()

By putting this type of logic in the View you breaking the Single responsibility principlerule in M (data), V (visual), C (logic).

通过将这种类型的逻辑放在视图中,您打破了M(数据)、V(视觉)、C(逻辑)中的单一责任原则规则。

回答by Soroush

Use:

用:

@{
    Layout = null;
 }

to get rid of the layout specified in _ViewStart.

摆脱_ViewStart 中指定的布局。

回答by Souhaib Lamine

I wanted to display the login page without the layout and this works pretty good for me.(this is the _ViewStart.cshtml file) You need to set the ViewBag.Title in the Controller.

我想显示没有布局的登录页面,这对我来说非常有用。(这是 _ViewStart.cshtml 文件)您需要在控制器中设置 ViewBag.Title。

@{
    if (! (ViewContext.ViewBag.Title == "Login"))
    {
        Layout = "~/Views/Shared/_Layout.cshtml";        
    } 
}

I know it's a little bit late but I hope this helps some body.

我知道现在有点晚了,但我希望这对某些人有所帮助。

回答by nsdiv

Just create the view as a partial view so that no layout file is used.

只需将视图创建为局部视图,以便不使用布局文件。

回答by Anand Gaikwad

Procedure 1 : Control Layouts rendering by using _ViewStart file in the root directory of the Views folder

步骤1:使用Views文件夹根目录下的_ViewStart文件控制布局渲染

This method is the simplest way for beginners to control Layouts rendering in your ASP.NET MVC application. We can identify the controller and render the Layouts as par controller, to do this we can write our code in _ViewStart file in the root directory of the Views folder. Following is an example shows how it can be done.

此方法是初学者在 ASP.NET MVC 应用程序中控制布局呈现的最简单方法。我们可以识别控制器并将布局渲染为标准控制器,为此我们可以在 Views 文件夹的根目录下的 _ViewStart 文件中编写我们的代码。以下是一个示例,展示了如何做到这一点。

 @{
 var controller = HttpContext.Current.Request.RequestContext.RouteData.Values["Controller"].ToString();
 string cLayout = "";
 if (controller == "Webmaster") {
 cLayout = "~/Views/Shared/_WebmasterLayout.cshtml";
 }
 else {
 cLayout = "~/Views/Shared/_Layout.cshtml";
 }
 Layout = cLayout;
 }

Procedure 2 : Set Layout by Returning from ActionResult

程序 2:通过从 ActionResult 返回来设置布局

One the the great feature of ASP.NET MVC is that, we can override the default layout rendering by returning the layout from the ActionResult. So, this is also a way to render different Layout in your ASP.NET MVC application. Following code sample show how it can be done.

ASP.NET MVC 的一大特色是,我们可以通过从 ActionResult 返回布局来覆盖默认布局呈现。因此,这也是一种在 ASP.NET MVC 应用程序中呈现不同布局的方法。以下代码示例显示了如何完成。

public ActionResult Index()
{
 SampleModel model = new SampleModel();
 //Any Logic
 return View("Index", "_WebmasterLayout", model);
}

Procedure 3 : View - wise Layout (By defining Layout within each view on the top)

程序 3:视图 - 明智的布局(通过在顶部的每个视图中定义布局)

ASP.NET MVC provides us such a great feature & faxibility to override the default layout rendering by defining the layout on the view. To implement this we can write our code in following manner in each View.

ASP.NET MVC 通过在视图上定义布局为我们提供了如此强大的功能和灵活性来覆盖默认布局呈现。为了实现这一点,我们可以在每个视图中以下列方式编写我们的代码。

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

Procedure 4 : Placing _ViewStart file in each of the directories

程序 4:在每个目录中放置 _ViewStart 文件

This is a very useful way to set different Layouts for each Controller in your ASP.NET MVC application. If we want to set default Layout for each directories than we can do this by putting _ViewStart file in each of the directories with the required Layout information as shown below:

这是为 ASP.NET MVC 应用程序中的每个控制器设置不同布局的一种非常有用的方法。如果我们想为每个目录设置默认布局,我们可以通过将 _ViewStart 文件放在每个目录中并包含所需的布局信息来实现,如下所示:

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