带有片段标识符的Response.Redirect在以后使用location.hash时会导致意外刷新 [英] Response.Redirect with a fragment identifier causes unexpected refresh when later using 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步骤:
- 用户访问A.aspx
- A.aspx使用Response.Redirect将用户带到B.aspx#house
- 在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
要求:
- 页面A必须重定向到页面B,并在URL中包含片段标识符
- 第B页上的任何用户操作都将设置location.hash
- 设置location.hash不能刷新B页
- 这必须适用于IE
- Page A must redirect to page B with a fragment identifier in the url
- Any user action on page B will set the location.hash
- Setting location.hash must not make page B refresh
- This must work on IE
注意:
- 仅在IE上进行错误的重新编译(在ie6 | 7 | 8上测试)。 Opera,FF,Chrome,Safari都没有重新加载的预期结果。
- 这个错误可能与ASP.NET无关,而且与IE无关。
- 对于任何愿意看这个的灵魂,我创建了一个最小的ASP.NET Web项目,以便于重现这里
- Bug only repros on IE (tested on ie6|7|8). Opera, FF, Chrome, Safari all have the expected results of no reload.
- This error may have nothing to do with ASP.NET, and everything to do with IE
- 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屋!