【发布时间】:2011-09-05 11:19:40
【问题描述】:
我知道这是不可能的,但是想要从 iFrame 执行 Response.Redirect 以重定向父页面的最佳选择是什么?
【问题讨论】:
标签: asp.net iframe redirect response.redirect
我知道这是不可能的,但是想要从 iFrame 执行 Response.Redirect 以重定向父页面的最佳选择是什么?
【问题讨论】:
标签: asp.net iframe redirect response.redirect
使用 ASP.NET 无法做到这一点。服务器端的 ASP.NET 可以重定向传入请求并且不知道父框架。
但是,如果您想在某些服务器端条件下重定向父框架,您可以像这样从服务器调用 JavaScript:
protected void Page_Load(object sender, EventArgs e) {
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"RedirectScript", "window.parent.location = 'http://yoursite.com'", true);
}
当然,您可以在客户端使用简单的 JavaScript window.parent.location = 'http://yoursite.com'。
【讨论】:
我刚刚成功使用了以下代码。它甚至绕过了X-Frame-Options SAMEORIGIN 并允许在 iframe 中从一个域重定向到另一个域:
string url = "https://siteurl.com";
Response.Write("<script>top.location='"+url+"';parent.location='"+url+"';</script>");
使用string interpolation(从 C# 6 开始):
string url = "https://siteurl.com";
Response.Write($"<script>top.location='{url}';parent.location='{url}';</script>");
【讨论】:
Response.Clear();
Header.Controls.Add(new LiteralControl(@"
<script type=""text/javascript"">
top.location = ""/Logout.aspx"";
parent.location = ""/Logout.aspx"";
</script>
"));
【讨论】:
url = "Your.asp?YourVar="&YourVar
%> \end your classic asp code and pass to the script below
<script type='text/javascript'>window.top.location.href = '<%= url %>'</scrip>
【讨论】: