可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
How come when I have Layout = null;
in my view - it still pulls in the default layout?!
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>
回答1:
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.
回答2:
I think this :
@{
Layout = "";
}
is not the same as this :
@{
Layout = null;
}
I use the second and it's working, no _Viewstart included.
回答3:
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.
回答4:
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;
回答5:
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).
回答6:
Use:
@{
Layout = null;
}
to get rid of the layout specified in _ViewStart.
回答7:
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.
回答8:
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";
}
回答9:
Just create the view as a partial view so that no layout file is used.