Reputation: 3316
I can't find the reel intentions behind MVC-Razor layouts through internet.
In shared folder, there is :
_Layout.cshtml
_LoginPartial.cshtml
Should i use the _Layout
for pages that dosen't require to be logged in, and use _LoginPartial
for pages that require to be logged in ? Or am i completely lost ?
To make it simple :
If i create a new view that can only be reached when logged in, should it be beginning with
Layout = "~/Views/Shared/_Layout.cshtml";
or
Layout = "~/Views/Shared/_LoginPartial.cshtml";
?
Edit :
Checking tutorials and explanation from everyone (thanks all)
_Layout.cshtml
is exactly like a master page in WEB FORM,
So i should always use :
Layout = "~/Views/Shared/_Layout.cshtml";
at the begining of a page i want to have formated like others.
The Login partial can be applied after authentification to alter the layout (disconnect button instead of connect, ect.)
Upvotes: 0
Views: 200
Reputation: 1228
The file _Layout.cshtml represent the layout of each page in the application. While partial view is a custom, reusable component that you can use in each page you need it. For example, we can create a partial view for customer and call it many time in page
<table class="table table-condensed">
@foreach (var student in Model.Students)
{
@Html.Partial("_StudentForm ", student)
}
</table>
So _Layout
is meant to be used for all pages and _LoginPartial.cshtml
can be used inside the page you need to have a login form in. Check this article about partial view
Tips and Tricks about Razor Partial Views
Upvotes: 1
Reputation: 655
Its more of a naming convention for layouts. views will inherit it from the viewstart file. If you look inside the layout file you will see the renderbody method.
@RenderBody()
This is where the HTML code is read and shown in the browser.
The same goes for the _loginPartial.cshtml
its just there for looks and to show you what Mvc is capable of.
Visual Studio creates the layout _Layout.cshtml when all but the Empty project template is used. This layout is applied to all views by default through the /Views/_ViewStart.cshtml file.
If you do not want the default layout applied to views, you can change the settings in _ViewStart.cshtml (or delete the file entirely) to specify another layout in the view, like this:
@{
Layout = "~/Views/Shared/MyLayout.cshtml";
}
Or you can disable any layout for a given view, like this:
@{
Layout = null;
}
Hope this helps.
Upvotes: 1
Reputation: 491
You can use the same _Layout.cshtml but your controller ActionMethod should change to Authenticate. Use the below link for more info.
Upvotes: 1
Reputation: 2254
Layout = "~/Views/Shared/_Layout.cshtml";
in your view start file (_ViewStart.cshtml), may times its the ONLY thing in that file.
@{
Layout = "~/Views/Shared/_Layout.cshtml";
}
the _Layout.cshtml and _Viewstart.cshtml combo is similar to a master page in web applications but these will not have controller actions associated with them. if you set the layout setting in the _ViewStart file you don't need to set it in your actual views, they will inherit it from the viewstart file. If you look inside the layout file you will see this line somewhere
@RenderBody()
That is where your individual views HTML will end up when your specific view is called.
The Login partial is just a quick start to demonstrate a view that can change display based on if the user is logged in or not.
Upvotes: 1