打印本文 打印本文 关闭窗口 关闭窗口
asp.net 中的截取 Session 变量(翻译)
作者:武汉SEO闵涛  文章来源:敏韬网  点击数2418  更新时间:2009/4/23 10:47:50  文章录入:mintao  责任编辑:mintao


   private void ReleaseRequestState(object sender, EventArgs eventArgs )
   {
    // This event is a good choice for apply filters to the output
    // stream to the browser/client.  By overriding the filter, you
    // can modify the output stream after all the response.write''''s are
    // done and before it gets to the browser.
    // Just add your own business rules to determine whether a filter
    // should be applied at all.
    try
    {
      if(HttpRes.ContentType != "text/html") { return; } 
     // HttpRes.Filter = new IISCapture.FilterHtml(ref CaptureSession,HttpRes.Filter);  
    }
    catch (Exception err) { ProcessError(err.Message); }
   }


// Custom methods


 private bool IsSessionInTechSupportMode()
 {
  bool Ret = false;
  try
  { 
    if (HttpApp.Context.Session[IISCapture.TroubleTicket.TechSupportEnable] != null)
    {
     if (HttpApp.Context.Session[IISCapture.TroubleTicket.TechSupportEnable].ToString() =="1")
     {
        Ret=true;
     }
    }
  }
  catch { }
  return Ret;
  }

  private bool IsSessionATechSupportRep()
  {
    bool Ret = false;
    try
    {
      if (HttpApp.Context.Session[IISCapture.TroubleTicket.TechSupportRep] != null)
       {
         if (HttpApp.Context.Session[IISCapture.TroubleTicket.TechSupportRep].ToString() =="1")
         {
            Ret=true;
         }
       }
    }
    catch { }
    return Ret;
  }

 

  private void ProcessError(string ErrMsg)
  {
    HttpApp.Context.Response.Write(ErrMsg);
  }

 
  public void Dispose(){}
 }
}

 

 
TroubleTicket.cs
using System;
using System.Web;
using System.Collections;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace IISCapture
{
 
 public class TroubleTicket
 {
 
   public const string TechSupportUserLastPage = "TechSupportUserLastPage";
   public const string TechSupportEnable = "TechSupportEnable";
   public const string TechSupportKey = "TechSupportKey";
   public const string TechSupportRep = "TechSupportRep";
   public const string TechSupportCache = "TechSupportCache_";
   public const string TechSupportCacheSession = "Session_";
   public const string TechSupportCacheForm = "Form_";
 

   public TroubleTicket()
   {
 
   }

 
   public void TransferToUrl(HttpContext oContext)
   {
     try
     {
      oContext.Response.Redirect(oContext.Session[TechSupportUserLastPage].ToString()); 
     }
     catch (Exception) { throw; }
   }
 
   public string LoadNewSessionToApplication(HttpContext oContext,string GotoUrl)
   {
      string TroubleTicket="";
      try
      {
        TroubleTicket = System.Guid.NewGuid().ToString();
        LoadSessionToApplication(oContext,TroubleTicket,GotoUrl);
        LoadFormToApplication(oContext,TroubleTicket);
      }
      catch (Exception) { throw; }
      return TroubleTicket;
   }
 
   public void LoadSessionToApplication(HttpContext oContext,string TroubleTicket,string GotoUrl)
   {
   
     try
     {
 
       oContext.Session[TechSupportUserLastPage] = GotoUrl;
       oContext.Session[TechSupportKey] = TroubleTicket;

       object[,] oValues = new object[2,oContext.Session.Keys.Count];

       for(int i=0;i<oContext.Session.Keys.Count;i++)
       {
          oValues[0,i] = oContext.Session.Keys[i].ToString();
          oValues[1,i] = oContext.Session[oContext.Session.Keys[i].ToString()];
       }

       oContext.Session[TechSupportEnable] = "1";

   if ((object[,])oContext.Cache[TechSupportCache + TechSupportCacheSession + TroubleTicket.Trim()] != null)
   {
     oContext.Cache.Remove(TechSupportCache + TechSupportCacheSession + TroubleTicket.Trim());
   }

   oContext.Cache.Insert(TechSupportCache + TechSupportCacheSession + TroubleTicket, _
         oValues,null,DateTime.MaxValue, TimeSpan.FromMinutes(10));
    
   }
   catch (Exception) { throw; }
   return;
  }
 
  public void LoadFormToApplication(HttpContext oContext,string TroubleTicket)
  {
    bool Found=false;
    try
    {
 
      object[,] oValues = new object[2,oContext.Request.Form.Keys.Count];

      for(int i=0;i<oContext.Request.Form.Keys.Count;i++)
      {
        oValues[0,i] = oContext.Request.Form.Keys[i].ToString();
        oValues[1,i] = oContext.Request[oContext.Request.Form.Keys[i].ToString()];
        Found=true;
      }
 
   if ((object[,])oContext.Cache[TechSupportCache + TechSupportCacheForm + TroubleTicket.Trim()] != null)
   {
     if (Found==true)
     {
       oContext.Cache.Remove(TechSupportCache + TechSupportCacheForm + TroubleTicket.Trim());
     }
   }

   oContext.Cache.Insert(TechSupportCache + TechSupportCacheForm + _
        TroubleTicket,oValues,null,DateTime.MaxValue, TimeSpan.FromMinutes(10));
    
   }
   catch (Exception) { throw; }
   return;
  }
 
 public string LoadSessionFromApplication(HttpContext oContext,string TroubleTicket)
 {
   string GotoUrl="";

   try
   {
   
    object[,] oValues = (object[,])oContext.Cache[TechSupportCache + _
                         TechSupportCacheSession + TroubleTicket.Trim()];
 
    for(int i=0;i<=oValues.GetUpperBound(1);i++)
    {
      oContext.Session[oValues[0,i].ToString()] = oValues[1,i]; 
    }

    oContext.Session[TechSupportEnable] = "0";
    oContext.Session[TechSupportRep] = "1";
    GotoUrl = oContext.Session[TechSupportUserLastPage].ToString();
    
   }
   catch (Exception) { throw; }
   return GotoUrl;
 }
 
 public object[,] GetFormFromApplication(HttpContext oContext,string TroubleTicket)
 {
   object[,] oValues = null;

   try
   {
     oValues = (object[,])oContext.Cache[TechSupportCache + TechSupportCacheForm + TroubleTicket.Trim()];
   }
   catch (Exception) { throw; }
   return oValues;
  }
 
 }
}

上一页  [1] [2] 

打印本文 打印本文 关闭窗口 关闭窗口