ashx页面中获取Session值出现“未将对象引用设置到对象的实例”错误

为了取得Session值,我在ashx页面中使用context.Session["uName"],居然出现错误: “未将对象引用设置到对象的实例”,查了资料才发现,要在ashx页面中取得session值还真得费一番周折。

第一步:先引入System.Web.SessionState命名空间:

using System.Web.SessionState;

第二步:页面类实现IRequiresSessionState接口: 

  /// <summary>
    /// 摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class AddUserInfo : IHttpHandler,IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {

                if(context.Session["uName"] != null)

      {

        string account = context.Session["uName"].ToString();

      }

    }

  }

这样就可以取到session值了。

posted @ 2017-02-27 16:34  益达915  阅读(215)  评论(0编辑  收藏  举报