MVC在session_end中获取会话[" x]值而没有this.Session和static?
问题描述:
Hi
我想知道如何在session_end(global.asax)中访问在控制器中分配的会话[xx]而不使用static或this.session并且特定实例(this.session)
Hi I would like to know how to access a session["xx"] assigned in a controller, in session_end(global.asax) without using "static" or "this.session" and which takes its specific instance (this.session )
答
您无需使用任何这些关键字子句来访问会话变量的值。只需使用它,
You do not need to be using any of these clauses of keywords to access the values of session variables. Just use this,
var val = Session["x"];
但由于变量可能不存在,而不是上面的代码,使用以下内容忽略任何运行时错误,
But since the variable might not exist, instead of above code, use the following to ignore any runtime errors,
if(Session["x"] != null) {
// variable exists
var val = Session["x"];
}
现在val将在x的Session变量中设置值。您可以在应用程序的任何页面中使用这些变量,它们在整个会话中都存在。
Now val would have the value set in the Session variable for x. You can use these variable in any page on the application, they exist throughout the session.