背景:C#使用 Response.Redirect("Welcome.aspx")进行页面跳转
问题:抛出异常 程序仍可以跑完
原因:Response.End 方法停止页的执行,并将该执行变换到应用程序的事件管线中的 Application_EndRequest 事件。
Response.End 后面的代码行将不执行。
在 Response.Redirect 和 Server.Transfer 方法中,都会内部调用 Response.End,停止网页的执行。
解决方案:
视具体情况使用下列方法之一:
- 对于 Response.End,调用 ApplicationInstance.CompleteRequest 方法而不调用 Response.End,以便跳过 Application_EndRequest 事件的代码执行。
- 对于 Response.Redirect,使用重载 Response.Redirect(String url, bool endResponse),将 endResponse 参数它设置为 false以取消对 Response.End 的内部调用。形如:Response.Redirect("Welcome.aspx",false)
- 对于 Server.Transfer,请改用 Server.Execute 方法。Transfer跳转以后执行跳转页的代码,Execute执行以后回到原页执行。