欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

*.ashx文件不能访问Session值的解决方法

程序员文章站 2024-02-24 12:24:10
本文实例讲述了*.ashx文件不能访问session值的解决方法。分享给大家供大家参考之用。具体方法如下: 实例代码如下: <%@ webhandler...

本文实例讲述了*.ashx文件不能访问session值的解决方法。分享给大家供大家参考之用。具体方法如下:

实例代码如下:

<%@ webhandler language="c#" class="producthandler" %>
using system; 
using system.web; 
using jbtcard.entity.companyentity; 
using jbtcard.business.companybs; 
using system.collections.generic; 
using newtonsoft.json; 
using system.web.sessionstate; 

public class producthandler : ihttphandler, irequiressessionstate, ireadonlysessionstate{ 
  
  public void processrequest (httpcontext context) { 
  string moduleid = context.request.form["moduleid"].tostring(); 
  string message = ""; 
  switch (moduleid) 
  { 
  case "getptypelist": 
  ilist<producttypeidnameentity> list = producttypebs.getptypelistidname(); 
  message = javascriptconvert.serializeobject(list); 
  break; 
  case "getcompanybyid": 
int cid = convert.toint32(context.session["cid"]);  companyentity company = companybs.getcompanyentybyid(1); 
  message = javascriptconvert.serializeobject(company); 
  break; 
  case "insert": 
  string condis = context.request.form["condi"].tostring(); 
  productentity model = (productentity)javascriptconvert.deserializeobject(condis, typeof(productentity)); 

  bool b = productbs.addproduct(model); 
  if (b) 
  { 
  message = "{success:true}"; 
  } 
  else 
  { 
  throw new exception("商品添加失败!"); 
  } 
break; 
  } 
  context.response.contenttype = "text/javascript"; 
  context.response.write(message); 
  } 

  public bool isreusable { 
    get { 
      return false; 
    } 
  } 
}

其实只要加上这句就好了:

using system.web.sessionstate;

希望本文所述对大家的asp.net程序设计有所帮助