28

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

royhowie
  • 11,075
  • 14
  • 50
  • 67
JBeckton
  • 7,095
  • 13
  • 51
  • 71

2 Answers2

60

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
Buildstarted
  • 26,529
  • 10
  • 84
  • 95
3

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"]