Response.Redirect() in iFrame, redirect parent window
I know this isn't possib开发者_JAVA百科le, but what is the best alternative for wanting to do Response.Redirect
from an iFrame, to redirect the parent page?
You can not do this using ASP.NET. ASP.NET on server side can redirect incoming request and can't know about a parent frame.
But if you want to redirect parent frame on some server side condition you can call JavaScript from server like this:
protected void Page_Load(object sender, EventArgs e) {
ClientScriptManager.RegisterClientScriptBlock(this.GetType(),
"RedirectScript", "window.parent.location = 'http://yoursite.com'", true);
}
And of course you can use simple JavaScript window.parent.location = 'http://yoursite.com' on client side.
I just used the following code with success. It even bypassed the X-Frame-Options SAMEORIGIN
and allows redirection from one domain to another one in an iframe:
string url = "https://siteurl.com";
Response.Write("<script>top.location='"+url+"';parent.location='"+url+"';</script>");
With string interpolation (since 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>
精彩评论