一聚教程网:一个值得你收藏的教程网站

热门教程

asp.net session 简单测试应用程序

时间:2022-06-25 04:21:09 编辑:袖梨 来源:一聚教程网

asp教程.net session 简单测试应用程序

<%@ page language="c#" autoeventwireup="true"  codefile="default.aspx.cs" inherits="_default" %>



    session测试
   


   


   
   
 
 
 session包含的值数量:[<%= session.count %>]
 <%= session["test"] %>
   

    当前session数:<%= constants.sessioncount %>

    累计session数:<%= constants.sessionallcount%>
   

   
   
   
   
   
    <% foreach (dictionaryentry r in constants.sessionlist) {
     bool iscur = r.key.tostring() == session.sessionid;  %>
    ;">
   
   
   
 <% } %>
   
sessionid启动时间
<%= r.key %><%= r.value %>

   

cs文件

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections;

public partial class _default : system.web.ui.page
{
    protected void page_load(object sender, eventargs e)
 {
  //注意当ajax请求aspx页面时,aspx页面应设置为不允许缓存!
  response.appendheader("pragma", "no-cache");
  response.appendheader("cache-control", "no-cache, must-revalidate");
  response.appendheader("expires", "0");
  if (request.querystring["ajax"] != null)
  {
   //ajax返回当前网站session数量
   response.write(constants.sessionlist.count.tostring());
   response.end();
  }
    }

 protected void button1_click(object sender, eventargs e)
 {
  //添加session
  //session["test"] = datetime.now;
  session.add("test", datetime.now);
  response.redirect(request.url.tostring());
 }

 protected void button2_click(object sender, eventargs e)
 {
  //移除指定session
  //session["test"] = null;
  //session.remove("test");

  //移除所有session 与 session.removeall();功能相同
  //session.clear();

  //终止当前回话状态 注意会触发session_end 与session超时效果相同
  //调用此句后,sessionid并没有被重置。
  //当仅仅调用此句时(当session超时过期时),如果再刷新当前页面则会一直调用session_start 和 session_end
  //除非给session添加值或重置sessionid后,才会退出此状态。
  session.abandon();

  //重置当前sessionid
  //如果设置的sessionid与已存在的sessionid重复,则将发生session劫持。
  //如不存在则会系统会自动创建新session
  //response.cookies.add(new httpcookie("asp.net教程_sessionid", this.textbox1.text));
  response.redirect(request.url.tostring());
 }
}

constanst.cs

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
using system.collections;

///


/// constants 的摘要说明
///

public class constants
{
 public constants()
 {
  //
  // todo: 在此处添加构造函数逻辑
  //
 }

 ///


 /// 当前所有session列表
 ///

 public static hashtable sessionlist = new hashtable();

 ///


 /// 当前session个数
 ///

 public static int sessioncount = 0;

 ///


 /// 累计session个数
 ///

 public static int sessionallcount = 0;
}

global.asax

<%@ application language="c#" %>

热门栏目