zoukankan      html  css  js  c++  java
  • session判断有效性讨论

    C#.NET判断某个Session存在与否,通常用的方法是Session["NO"]==null来判断
    但这个句子不是所有时刻都是对的.这样还是会偶尔出错的。
    如果该请求还没有实例化一个HttpSessionState,也就是第一次访问时执行该代码最有可能,你可以试试在Global中Application_BeginRequest事件中执行该代码。
    private void Appliction_BeginRequest(object sender, EventArgument e)
    {
    if(HttpApplication.HttpContext.Current.Session["No"]!=null)
    {
    //这里放执行程序
    }
    }
    编译后第一次访问将会发生异常,HttpSessionState抛出这个常见的异常:没有该对象的实例
    当然在页面程序上是不会出现这种问题的。:)
    不过如果要在Global中或者HttpModule,HttpHandler中执行这些代码的话就要考虑了。

    解决方法:
    1、先引用System.Web.SessionState 这个命名空间,
    2、如果是要在HttpHandler 中读取Session的内容,就要在实现IHttpHandler 的类中同时实现IReadOnlySessionState 这个接口。
    3、如果是要在HttpHandler 中读写Session的内容,就要在实现IHttpHandler 的类中同时实现IRequiresSessionState
    具体代码:
  • 相关阅读:
    jquery----->helloworld
    hibernate------->第一个程序
    spring使用jdbcTemplate和jdbcdaosupport和namedparameter
    spring--------------->AOP
    spring------>Helloworld
    JS全选
    表单重复提交
    session
    cookies
    83. Remove Duplicates from Sorted List
  • 原文地址:https://www.cnblogs.com/wzyexf/p/703582.html
Copyright © 2011-2022 走看看