我的解决方案中包含了若干类库和一个网站,网站添加对类库项目的引用!现在的问题是有的时候需要在类库中使用session等变量!在web层中架构了一些web service然后在需要使用session的类库中添加web引用,获取application变量没有问题,但是获取用户session时却怎么也取不到改注意的地方都注意了配置也做到了!web.config配置如下(部分):
<system.web>
<sessionState mode="InProc" />
</system.web> [WebMethod(EnableSession=true)]
public object GetSession(string ses)
{
object obj=Session[ses];
return obj;
}使用post访问没有问题 然而从类库中调用这个web 方法时却总时null不知道如何解决 请高手出面知道!
<system.web>
<sessionState mode="InProc" />
</system.web> [WebMethod(EnableSession=true)]
public object GetSession(string ses)
{
object obj=Session[ses];
return obj;
}使用post访问没有问题 然而从类库中调用这个web 方法时却总时null不知道如何解决 请高手出面知道!
HttpSessionState Session;
HttpServerUtility Server;
HttpRequest Request;
HttpResponse Response; 最后我们可以在构造函数里做如下处理:
try
{
Session = ((System.Web.UI.Page)parent).Session;
Server = ((System.Web.UI.Page)parent).Server;
Request = ((System.Web.UI.Page)parent).Request;
Response = ((System.Web.UI.Page)parent).Response;
}
catch
{
Server = ((System.Web.UI.UserControl)parent).Server;
Session = ((System.Web.UI.UserControl)parent).Session;
Request = ((System.Web.UI.UserControl)parent).Request;
Response = ((System.Web.UI.UserControl)parent).Response;
}
以上做完,你就可以在类库里直接使用这几种对象了。
在类库中是无法添加对sessionstate命名空间引用的!
---------------------------------------------
首先我们要在类文件里引用using System.Web;using System.Web.SesstionState(这个地方写错了,应该是SessionState,写成SesstionState了);命名空间,然后是对这种对象的声明:
using System.Web.SessionState;
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.SessionState;namespace WebCube.Base
{
public class Class1
{
HttpSessionState Session;
public Class1(object parent)
{
try
{
Session = ((System.Web.UI.Page)parent).Session;
}
catch
{
Session = ((System.Web.UI.UserControl)parent).Session;
}
}
}
}
等等 parent是什么变量
---------------------------------------
parent是页面对象参数
添加对 System.Web.dll 的引用