Building an MVC 3 app with Razor and I have some information persisted in the Session scope that will be used in the _Layout file.
I have no clue as to what is the best way to implement this. Some of this information is used to determine what is rendered in the header.
I have a CurrentUser object stored in Session scope
So, the _layout. cshtml would be a layout view of all the views included in Views and its subfolders. Setting Layout View in _ViewStart.cshtml. The _ViewStart. cshtml can also be created in the sub-folders of the View folder to set the default layout page for all the views included in that particular subfolder.
Layout pages are typically named _Layout. cshtml, the leading underscore preventing them from being browsed directly. Standard practice is to specify the layout page in a _ViewStart. cshtml file, which affects all content pages in the folder in which it is placed, and all subfolders.
You could just access the HttpContext in the layout file
@HttpContext.Current.Session["Whatever"].ToString()
or, if you want access to the user object you could just create an object in the page and assign it
@{ CurrentUser user = (CurrentUser)HttpContext.Current.Session["CurrentUser"]; }
Then later in your code...
@user.Name
An easier way to do it is using Session
property directly from the view (HttpContext.Current.
prefix should not be necessary at all):
@(CurrentUser)Session["CurrentUser"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With