C#调用Response.Redirect方法触发异常问题

来源:互联网 发布:淘宝货源数据包 编辑:程序博客网 时间:2024/06/14 22:20

C#调用Response.Redirect方法触发异常问题

   

    今天在后台编写一个用户验证功能的时候,需要根据用户的不同权限进行不同的页面跳转,使用的跳转语句如下:
    Response.Redirect(“LogonIn.aspx”);

 

    但是,总是提示下面这个异常:Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

    到网上查了一下,发现是方法的问题,换用其重载方法Response.Redirect(“LogonIn.aspx”, false);后即可正常执行。
   
    以下是查到的原因(看的不是太懂……):
    Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件,Response.End 后面的代码行将不执行。此问题出现在 Response.Redirect 和 Server.Transfer 方法中,这是由于这两种方法都在内部调用 Response.End
   
    相应的解决办法如下:


(1)    对于 Response.End:
    调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。

 

(2)    对于Response.Redirect:
    使用重载 Response.Redirect(String url, bool endResponse),对 endResponse 参数它传递 false以取消对 Response.End 的内部调用。例如:
Response.Redirect ("nextpage.aspx", false);
如果使用这种解决方法,Response.Redirect 后面的代码将得到执行。

(3)    对于 Server.Transfer:
    请改用 Server.Execute 方法。

原创粉丝点击