【问题标题】:Response.Redirect() in iFrame, redirect parent windowiFrame中的Response.Redirect(),重定向父窗口
【发布时间】:2011-09-05 11:19:40
【问题描述】:

我知道这是不可能的,但是想要从 iFrame 执行 Response.Redirect 以重定向父页面的最佳选择是什么?

【问题讨论】:

    标签: asp.net iframe redirect response.redirect


    【解决方案1】:

    使用 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'。

    【讨论】:

    • 请注意,框架和父框架文档的域需要匹配,以避免大多数浏览器抛出拒绝访问错误。
    • 如果我的父母在框架中有另一个域?
    【解决方案2】:

    我刚刚成功使用了以下代码。它甚至绕过了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>");
    

    【讨论】:

      【解决方案3】:
      Response.Clear();
      Header.Controls.Add(new LiteralControl(@"
      <script type=""text/javascript"">
      top.location = ""/Logout.aspx"";
      parent.location = ""/Logout.aspx"";
      </script>
      "));
      

      【讨论】:

        【解决方案4】:
            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>
        

        【讨论】:

          猜你喜欢
          • 2014-12-30
          • 2011-02-23
          • 2014-02-16
          • 1970-01-01
          • 2023-04-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多