没有布局的剃刀视图

为什么当我有 Layout = null;在我的视图-它仍然拉在默认的布局? !

有什么诀窍能阻止它这么做吗?

以下是我没有布局的视图:

@{
Layout = "";
}


<!DOCTYPE html>


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

这是渲染后的输出! !

<!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>
140067 次浏览

Do you have a _ViewStart.cshtml in 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/Shared directory, 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.

You (and KMulligan) are misunderstanding _ViewStart pages.

_ViewStart will always execute, 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).

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

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

I think this :

@{
Layout = "";
}

is not the same as this :

@{
Layout = null;
}

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

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

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...

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

@{
Layout = null;
}

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

Put "../" before of the url

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

Regards;

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

Your Controller Action should:

return PartialView()

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

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.

@{
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.

Use:

@{
Layout = null;
}

to get rid of the layout specified in _ViewStart.

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

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.

 @{
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

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.

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)

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.

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

Procedure 4 : Placing _ViewStart file in each of the directories

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:

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

If you are working with apps, try cleaning solution. Fixed for me.

@{ viewbag.title="index" Layout = null; }