我自己写一个类,在里面创建一个sessionpublic class a
{
System.Web.HttpContext.Current.Session["UserCode"] = "abc";
}编译通过,运行时系统提示"未将对象引用设置到对象实例",问题是我就是要创建这个session,又如何先实例化?在page中不是都这么用么?谢谢!

解决方案 »

  1.   

    继承IRequiresSessionState接口,添加System.Web.SessionState引用
    如:
    using System.Web.SessionState;public class a:IRequiresSessionState
      

  2.   

    谢谢帮助
    我修改了程序如下:using System.Web;
    using System.Web.UI;
    using System.Web.SessionState;
    public class a : IRequiresSessionState{
    System.Web.HttpContext.Current.Session["UserCode"] = "abc";
    }可编译时系统提示:引用的类“XXXX”具有在未被引用的程序集中定义的基类或接口“System.Web.SessionState.IRequiresSessionState”。必须添加对程序集“System.Web”的引用。改成继承page类,系统提示:引用的类“XXXX”具有在未被引用的程序集中定义的基类或接口“System.Web.UI.Page”。必须添加对程序集“System.Web”的引用。我都已经引用了,什么原因?谢谢
      

  3.   

    类库project是默认不引用System.Web的,你要像添加其他dll引用那样手工添加一次。
      

  4.   

    我的project里已经引用System.Web了,还是这样,谢谢!
      

  5.   

    嗯,對。沒錯兒。
    我勸樓主還是學java吧!
    .net這玩意兒,開發工具bug也多,也不太穩定。尤其是vs2005把什麽東西都給隱藏起來了,像vb似的,所以一旦出現問題的話,就不知道怎樣來處理了。真費勁。
      

  6.   

    using System;
    using System.Web;
    using System.Configuration;
    namespace ITExpert
    {
    /// <summary>
    /// SysBasePage 的摘要说明。
    /// </summary>
    public class SysBasePage :System.Web.UI.Page
    { public SysBasePage():base()
    {
    //
    // TODO: 在此处添加构造函数逻辑
    //
    } /// <summary>
    /// 
    /// </summary>
    protected override void OnInit(EventArgs e)
    {
    base.OnInit(e);
    SecurityValidation();
    } protected static void SecurityValidation()
    {
    string RedirectPage =  ConfigurationSettings.AppSettings["SYS_ROOT"].ToString() + "login.aspx";


    if(HttpContext.Current.Session["UserID"]==null)
    {
    HttpContext.Current.Response.Redirect(RedirectPage);
    //HttpContext.Current.Response.Write("<script>"+"window.parent.location.href='" +RedirectPage+"';"+"</script>");
    }
    if (HttpContext.Current.Session["GroupRight"]==null)
    {
    HttpContext.Current.Response.Redirect(RedirectPage);
    //HttpContext.Current.Response.Write("<script>"+"window.parent.location.href='" +RedirectPage+"';"+"</script>");
    }
    } }
    }