当前位置:Gxlcms > asp.net > *.ashx文件不能访问Session值的解决方法

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

时间:2021-07-01 10:21:17 帮助过:10人阅读

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

实例代码如下:

  1. <%@ WebHandler Language="C#" Class="productHandler" %>
  2. using System;
  3. using System.Web;
  4. using JBTCard.Entity.CompanyEntity;
  5. using JBTCard.Business.CompanyBS;
  6. using System.Collections.Generic;
  7. using Newtonsoft.Json;
  8. using System.Web.SessionState;
  9. public class productHandler : IHttpHandler, IRequiresSessionState, IReadOnlySessionState{
  10. public void ProcessRequest (HttpContext context) {
  11. string moduleId = context.Request.Form["moduleId"].ToString();
  12. string message = "";
  13. switch (moduleId)
  14. {
  15. case "getPTypeList":
  16. IList<ProductTypeIdNameEntity> list = ProductTypeBS.GetPTypeListIdName();
  17. message = JavaScriptConvert.SerializeObject(list);
  18. break;
  19. case "getCompanyById":
  20. int cid = Convert.ToInt32(context.Session["cid"]); CompanyEntity company = CompanyBS.GetCompanyEntyById(1);
  21. message = JavaScriptConvert.SerializeObject(company);
  22. break;
  23. case "insert":
  24. string condis = context.Request.Form["condi"].ToString();
  25. ProductEntity model = (ProductEntity)JavaScriptConvert.DeserializeObject(condis, typeof(ProductEntity));
  26. bool b = ProductBS.AddProduct(model);
  27. if (b)
  28. {
  29. message = "{success:true}";
  30. }
  31. else
  32. {
  33. throw new Exception("商品添加失败!");
  34. }
  35. break;
  36. }
  37. context.Response.ContentType = "text/javascript";
  38. context.Response.Write(message);
  39. }
  40. public bool IsReusable {
  41. get {
  42. return false;
  43. }
  44. }
  45. }

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

  1. using System.Web.SessionState;

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

人气教程排行