我想用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;
}
}
}
}
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;
}
}
}
}
解决方案 »
- c#.net表单提交的简单问题
- 有没有哪位大虾实现了用treeview显示森林的?
- FreeTextBox上传图片路径问题,请高手解答,3Q!!!
- sqldatasource控件当如何使用?
- 求 提交表单的隐藏与显现问题,看看能实现不?来者有分,虽然我的分已经很少了
- 为什么我动态生成的按扭的自定义事件不能完全执行
- "GDI+ 中发生一般性错误" 请高手解决! 错误信息:执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及
- DataGrid 显示问题
- 在使用C#操作Word的时候,有一点关于Word布局的问题
- 有关web页面打印的问题
- 关于vs2010 sp1有没有mvc3的问题
- ASP.NET中数据分类显示
能catch到。就是没有办法Response.Redirect("http://www.baidu.com",true);后面的代码也没有执行,但是也没有跳转。哭啊,在线等。
public class MyModule :System.Web.UI.Page, IHttpModule
但也没有作用
这个错误。
{
Response.Redirect("",false);
}
catch (System.Threading.ThreadAbortException e)
{
throw;
}
哦,晕了 Response.Redirect(); 好像不能跳到域外面去。 只能在你的站内跳。例如从 /a.aspx 跳到 b.aspx
2.判断session为空不要写在catch中捕获异常,直接if(session["id"]<>NULL) or(session["id"].ToString!="")来进行分支判断.
产生新的CONTEXT
Redirect是客户端的方法,好处是浏览器知道是传的,历史记录也会被更新,缺点是会影响性能,因为它还要与服务器回发
我先HttpContext.Current.Session.RemoveAll()了里面的数据。然后再读session,读不到就catch,跳转。
改成这个~~
Response.End()
试一下
结果发现原来是我的web.config配置有问题..可能是因为你在web.config那里禁止了匿名用户打开你登录之后的页面;
如果你重新写了一个成员资格提供程序类,也有可能是你没有指定你自己写的成员资格程序提供类作为你默认的成员资格提供类。