IIS ASP.NET上的Session对象为null [英] Session object is null on IIS ASP.NET

查看:407
本文介绍了IIS ASP.NET上的Session对象为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序。我将用户信息存储到会话中。我将它作为一个网站部署在IIS上是可以的。但是当我将它作为默认网站的应用程序部署在IIS上时,会话对象始终为null。

它在.NET v4.5应用程序池下运行。我的IIS版本是8.0。为什么会话对象在IIS上为空?我坚持这个问题。



谢谢。



我尝试过:



public int GetUserId()

{

int userId = 0;



var userObj =(Models.User)Session [user];



if(userObj!= null )

{

userId = userObj.UserId;

}

返回userId;

$





userObj为null并将userId返回为0.

解决方案

您没有共享代码来设置会话变量的值。你这样做了吗?

  //  创建一个新的用户并保存 
会话[ user] = new User();

// 访问此处的值



如果在获取值之前执行此操作,则只有这样才能从会话中获取值。这正是为什么建议在尝试获取值的语句周围使用条件块的原因。此外,可以通过编程方式删除会话变量,确保您的应用程序不刷新变量。如果是,请删除这些陈述。 Session.Clear()等等。这些函数用于在不再需要时清除值,在会话到期之前



如需更多讨论和概述,请阅读: ASP.NET会话状态概述 [ ^ ]

HttpContext。会话属性(System.Web) [ ^ ]

.net - C#清除会话 - 堆栈溢出 [ ^ ]


I have a web application. I stored user information to session. It is ok when I deploy it on IIS as a website. But when I deploy it on IIS as an application of Default Website, the session object is always null.
It's run under the .NET v4.5 Application pool. My IIS version is 8.0. Why Session Object is null on IIS? I'm stuck on this issue.

Thanks.

What I have tried:

public int GetUserId()
{
int userId = 0;

var userObj = (Models.User)Session["user"];

if (userObj != null)
{
userId = userObj.UserId;
}
return userId;
}


userObj is null and return userId as 0.

解决方案

You did not share the code to set the value for the session variable. Did you do this?

// Create a new user and save it
Session["user"] = new User();

// Access the value here


If you did this before getting the value, only then you can get the value back from the session. This is exactly why it is recommended to use a conditional-block around the statements where you try to get the values. Also, Session variables can be removed programmatically, make sure your application is not refreshing the variables. If it is, please remove those statements. Session.Clear() etc. such functions are meant to clear the values once they are no longer needed, before the session expires.

For more discussion and overviews please read: ASP.NET Session State Overview[^]
HttpContext.Session Property (System.Web)[^]
.net - C# Clear Session - Stack Overflow[^]


这篇关于IIS ASP.NET上的Session对象为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆