我正在使用 ASP.NET Webforms C# 3.5 并利用 Response.Redirect 在事件调用后重定向页面。
如果我提交请求,第一个请求可以正常工作。我目前正在使用 Response.Redirect。 我尝试使用 Server.Transfer 并将 True 和 False 作为第二个参数传递给 Redirect 函数,并尝试关闭 SmartNavigation。
如果我单击我的提交按钮,该页面将按预期运行,并且 anchor 会将页面强制到正确的位置。如果我然后再次单击提交,将返回相同的 URL,并且浏览器除了坐下并“挂起”外什么都不做。如果我从我的 URL 中删除散列部分,重定向工作正常。
问题似乎只有在请求被重定向到带有 anchor 的当前 URL 并且使用 anchor (哈希符号)发生新的重定向时才会出现
Response.Redirect(/Script.aspx?param1=something¶m2=something#anchor);
我也试过这个:
Response.Redirect(/Script.aspx?param1=something¶m2=somethinganchor)
;
无论哪种情况,删除 anchor 都能解决问题。
此问题出现在 Chrome 和 Firefox 中。 Firebug 报告请求完成并显示响应字符串,所有内容看起来都应该带有井号,但浏览器只是挂起。挂起是指将光标显示为进度\正在做某事光标类型。
最佳答案
这是 Response.Redirect 的常见问题。
你可以设置
Response.Headers.Add("Location", "/Script.aspx?param1=something#anchor");
Response.Status = "301 Moved Permanently"; //or whatever status code you want to deliver, 302 or 307 are standard for Response.Redirect(...)
或者您可以将其作为附加查询字符串参数传递并使用 javascript 附加 anchor :
//page
Response.Redirect("/Script.aspx?param1=something&anchor=anchor");
//script.aspx
void Page_Init(object sender, EventArgs e)
{
ClientScript.RegisterClientScriptBlock(typeof(string), "anchor", "<script type=\"text/javascript\">location.href = location.href + '#" + Request["anchor"] + "';</script>");
}
关于c# - Response.Redirect - 如果当前页面具有带哈希的目标 URL 的 URL,则使用哈希发送重定向时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6130904/