我想用IHttpModule拦截用户访问,所以写了一个类public class MyModule : IHttpModule.如果用户session为空就跳转,里面有一个方法如下。但是没有效果,请问什么原因,调试了发现有一个异常。"由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值",按照别人的解决方案还是不行。例如HttpContext.Current.Response.Redirect("/login.html",true); 换成HttpContext.Current.Response.Redirect("/login.html",false);和去掉try catch等,但问题依旧,请问如何处理,我用的wcf+linq+ext。 
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ConairOAS.Common;
using System.Web.UI.Design;
namespace ConairOAS.Common
{
    public class ConairModule : IHttpModule
    {
        public static int loginflag = 0;       
        public void Init(HttpApplication application)
        {            application.AcquireRequestState += (new
            EventHandler(this.Application_AcquireRequestState));
        }
        private void Application_AcquireRequestState(Object source,
                EventArgs e)
{            
   HttpApplication Application = (HttpApplication)source;
            HttpContext context = HttpContext.Current;
            HttpResponse Response = context.Response;
            Response.Redirect("http://www.baidu.com");//放在这里会有效果            Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);          
            string path = context.Request.Url.AbsolutePath.ToLower();             
            if (path.Contains("index.aspx") && loginflag == 0)
            {
                Response.Redirect("/login.html");
            }
             if (path.Contains(".svc/"))
            {
                if (path.Contains("login.svc/validateuser"))
                {                    
                    loginflag++;
                }
                else if (path.Contains("logout.svc/clearusersession"))
                {
                    loginflag = 0;                    
                }
                else
                {
                    loginflag++;
                    Response.Redirect("http://www.baidu.com");//放在这里没有效果                    string objectSid = checkSession(context);//session验证                 
                    
                }
            }                        
   }        
         public string checkSession(HttpContext context)
        {
            string objectSid ;
            try
            {                
                objectSid = context.Session["objectSid"].ToString();
                return objectSid;
            }
            catch(Exception ex)
            {
              Response.Redirect("http://www.baidu.com");//放在这里也没有效果                            
                throw ex;
            }           
            
        }
        }
}
 

解决方案 »

  1.   

    设个断点,调试一下试一试Server.Transfer呢Response.Redirect("http://www.baidu.com",true);
      

  2.   

    catch访问不到吧,session为空不会异常吧
      

  3.   

    try:public class ConairModule : IHttpModule -->public class ConairModule : Page,IHttpModule
      

  4.   

    to ershou007  
    能catch到。就是没有办法Response.Redirect("http://www.baidu.com",true);后面的代码也没有执行,但是也没有跳转。哭啊,在线等。
      

  5.   

    必定是你的判定逻辑有问题另外你那个checkSession也是不妥的。
      

  6.   

    to zpcoder ,你说的这个方法我也试过了。
     public class MyModule :System.Web.UI.Page, IHttpModule
    但也没有作用
      

  7.   

    + [System.Threading.ThreadAbortException] {由于代码已经过优化或者本机框架位于调用堆栈之上,无法计算表达式的值。} System.Threading.ThreadAbortException
    这个错误。
      

  8.   

    把Response.Redirect();语句从TRY中拿出
      

  9.   

    try 

    Response.Redirect("",false); 

    catch (System.Threading.ThreadAbortException e) 

    throw; 

      

  10.   


     哦,晕了 Response.Redirect();   好像不能跳到域外面去。 只能在你的站内跳。例如从 /a.aspx 跳到 b.aspx
      

  11.   

    Response.Redirect();  可以跳转的,Server.Transfer则不行。
      

  12.   

    发现当处理了部分source的结果后,就无法跳转,如果没有处理,就可以直接跳转。
      

  13.   

    没有继承自IRequiredSessionState接口的类无法读写Session。
      

  14.   

    我这里可以读session啊,只是跳转不成功而已。请问如何跳转呢?有没有什么好的见解?
      

  15.   

    1.逻辑有问题,试验数据是什么?
    2.判断session为空不要写在catch中捕获异常,直接if(session["id"]<>NULL) or(session["id"].ToString!="")来进行分支判断.
      

  16.   

    Response.Redirect     Response.Redirect方法导致浏览器链接到一个指定的URL。当Response.Redirect()方法被调用时,它会创建一个应答,应答头中指出了状态代码302(表示目标已经改变)以及新的目标URL。浏览器从服务器收到该应答,利用应答头中的信息发出一个对新URL的请求。     这就是说,使用Response.Redirect方法时重定向操作发生在客户端,总共涉及到两次与服务器的通信(两个来回):第一次是对原始页面的请求,得到一个302应答,第二次是请求302应答中声明的新页面,得到重定向之后的页面。   
    产生新的CONTEXT
    Redirect是客户端的方法,好处是浏览器知道是传的,历史记录也会被更新,缺点是会影响性能,因为它还要与服务器回发
      

  17.   

    to only_endure  逻辑没有问题。
    我先HttpContext.Current.Session.RemoveAll()了里面的数据。然后再读session,读不到就catch,跳转。
      

  18.   

    httpcontent.current.response.redirect();
    改成这个~~
      

  19.   

    后面加一个
    Response.End()
    试一下
      

  20.   

    我也遇到过类似问题..
    结果发现原来是我的web.config配置有问题..可能是因为你在web.config那里禁止了匿名用户打开你登录之后的页面;
    如果你重新写了一个成员资格提供程序类,也有可能是你没有指定你自己写的成员资格程序提供类作为你默认的成员资格提供类。
      

  21.   

    Response.Redirect("http://www.baidu.com");  这样可以跳转啊! 
      

  22.   

     刚才看了你的问题,如果真的没法解决,可以增加一个跳板! 先让它跳转到站内,然后在page_load里面进行外跳!
      

  23.   

    System.Threading.ThreadAbortException 这个错误是每次Response.Redirect 或server.transfer都会有的。这个exception应该过滤掉。就是不要throw出来,要不然你的catch段执行就影响了你的页面跳转。