如標題
1. 建一隻basepage 其他的...aspx繼承這支basepage
2.basepage繼承 System.Web.UI.Page
3. 複寫 basepage onload方法
public class BasePage : System.Web.UI.Page
{
protected override void OnPreLoad(EventArgs e)
{
string msg = "666666!!";
string gourl = "http://google.com.tw";
if (HttpContext.Current.User.Identity == null)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("<script>alert(\"" + msg.Replace("\"", "\\\"") + "\");window.location.href='" + gourl + "'</script>");
NoExceptionResponseEnd();
}
if (User.Identity.IsAuthenticated == false) //驗證是否有登入過
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Write("<script>alert(\"" + msg.Replace("\"", "\\\"") + "\");window.location.href='" + gourl + "'</script>");
NoExceptionResponseEnd();
}
base.OnPreLoad(e);
}
/// <summary>
/// 避免 ThreadAbortException 的Response.End() 替代寫法
/// </summary>
public void NoExceptionResponseEnd()
{
// https://blog.darkthread.net/blog/response-end-alternative/
//https://stackoverflow.com/a/22363396/288936
//將Buffer中的內容送出
HttpContext.Current.Response.Flush();
//忽視之後透過Response.Write輸出的內容
HttpContext.Current.Response.SuppressContent = true;
//忽略之後ASP.NET Pipeline的處理步驟,直接跳關到EndRequest
HttpContext.Current.ApplicationInstance.CompleteRequest();
}
}
資料來源參考如下
ASP.NET页面跳转
https://www.geek-share.com/detail/2482735161.html
黑暗大大說明 response end的替代方式
https://blog.darkthread.net/blog/response-end-alternative/
以上文章僅用紀錄資料使用.....