带有片段标识符的Response.Redirect在以后使用location.hash时会导致意外刷新 [英] Response.Redirect with a fragment identifier causes unexpected refresh when later using location.hash

查看:136
本文介绍了带有片段标识符的Response.Redirect在以后使用location.hash时会导致意外刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人可以协助描述我在IE上的ASP.NET网站上遇到的以下问题的解决方案。在下文中,我将描述错误并澄清所需解决方案的要求。

I was hoping someone can assist in describing a workaround solution to the following issue I am running into on my ASP.NET website on IE. In the following I will describe the bug and clarify the requirements of the needed solution.

Repro步骤:


  1. 用户访问A.aspx

  2. A.aspx使用Response.Redirect将用户带到B.aspx#house

  3. 在B.aspx#house上,用户点击设置window.location.hash ='test'的按钮

实际结果:

B.aspx再次加载。该网址现在显示B.aspx #test

B.aspx is loaded again. The URL now shows B.aspx#test

预期结果:

否重装。该网址只会更改为B.aspx #test

No reload. The URL will just change to B.aspx#test

要求:


  1. 页面A必须重定向到页面B,并在URL中包含片段标识符

  2. 第B页上的任何用户操作都将设置location.hash

  3. 设置location.hash不能刷新B页

  4. 这必须适用于IE

  1. Page A must redirect to page B with a fragment identifier in the url
  2. Any user action on page B will set the location.hash
  3. Setting location.hash must not make page B refresh
  4. This must work on IE

注意:


  1. 仅在IE上进行错误的重新编译(在ie6 | 7 | 8上测试)。 Opera,FF,Chrome,Safari都没有重新加载的预期结果。

  2. 这个错误可能与ASP.NET无关,而且与IE无关。

  3. 对于任何愿意看这个的灵魂,我创建了一个最小的ASP.NET Web项目,以便于重现这里

  1. Bug only repros on IE (tested on ie6|7|8). Opera, FF, Chrome, Safari all have the expected results of no reload.
  2. This error may have nothing to do with ASP.NET, and everything to do with IE
  3. For any kind soul willing to have a look at this, I have created a minimal ASP.NET web project to make it easy to repro here


推荐答案

如果有一条禁止在重定向后页面上进行缓存的指令,这将在IE中发生。如果你删除No-Cache标题,你会发现问题不再是重复。

This will happen in IE if there's a directive that forbids caching on the post-redirection page. If you remove the No-Cache header, you will find that the problem no longer repros.

这是一个 Meddler脚本,用于演示行为:

Here's a Meddler script that demonstrates the behavior:

import Meddler;
import System;
import System.Net.Sockets;
import System.Windows.Forms;

class Handlers
{
    static function OnConnection(oSession: Session)
    {
        if (oSession.ReadRequest())
        {       
            var oHeaders: ResponseHeaders = new ResponseHeaders();
            oHeaders["Connection"] = "close";

            if (oSession.urlContains("postRedir.asp"))
            {
                oHeaders.Status = "200 OK";
                oHeaders["Content-Type"] = "text/html"; 
                oHeaders["Cache-Control"] = "no-cache";
                oSession.WriteString(oHeaders);
                oSession.WriteString("");
                oSession.WriteString("Top of page<br /><br /><br /><br /><br /><br />");
                oSession.WriteString("<br /><br /><br /><br /><div id='firstAnchor'>Div with ID firstAnchor</div>Click this link: <A href='#secondAnchor'>#secondAnchor</A>");
                oSession.WriteString("<br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />");
                oSession.WriteString("<br /><br /><br /><br /><br /><br /><br /><br /><br /><div id='secondAnchor'>Div with ID secondAnchor</a>");

            }
            else
                if (oSession.urlContains("doRedir.asp"))
                {
                    oHeaders.Status = "301 Redir";
                    oHeaders["Location"] = "/postRedir.asp#firstAnchor";                        
                    oSession.WriteString(oHeaders.ToString());          
                }
            else
            {
                oHeaders.Status = "200 OK";
                oHeaders["Content-Type"] = "text/html"; 
                oSession.WriteString(oHeaders);
                oSession.WriteString("This is a test case for http://stackoverflow.com/questions/1985056/response-redirect-with-a-fragment-identifier-causes-unexpected-refresh-when-later<BR/>");
                oSession.WriteString("Click this link: <A href='doRedir.asp'>Do Redirect</A>");
                oSession.WriteString("<form action='doRedir.asp' method='post'><input type=submit value='Submit as form' /></form>");
            }
        }

        oSession.CloseSocket();
    }
}

这篇关于带有片段标识符的Response.Redirect在以后使用location.hash时会导致意外刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆