从剃刀_Layout.cshml访问Session对象 [英] Accessing a Session object from Razor _Layout.cshml
问题描述
建设有一个剃刀MVC 3应用程序,我在将在_layout文件中使用Session范围的一些信息仍然存在。
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.
我已经存储在会话范围的currentUser对象
I have a CurrentUser object stored in Session scope
推荐答案
您可以只访问的HttpContext在布局文件
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"]; }
再后来在code ...
Then later in your code...
@user.Name
这篇关于从剃刀_Layout.cshml访问Session对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!