Response.Redirect - 问题如果当前页面具有带有哈希的目标URL的URL,则使用哈希发送重定向

Jos*_*eld 3 c# asp.net .net-3.5

我正在使用ASP.NET Webforms C#3.5并在事件调用后使用Response.Redirect重定向页面.

如果我提交请求,则第一个请求正常.我目前正在使用Response.Redirect.我尝试使用Server.Transfer并将True和False作为第二个参数传递给Redirect函数,并尝试关闭SmartNavigation.

如果我单击我的提交按钮,页面会执行预期的操作,并且锚点将页面强制到正确的位置.如果我再次单击"提交",则返回相同的URL,浏览器不会执行任何操作,只能坐下来"挂起".如果我从我的URL中删除散列部分,重定向工作正常.

如果使用锚将请求重定向到当前URL并且使用锚点(哈希符号)发生新的重定向,则似乎只会出现此问题

Response.Redirect(/Script.aspx?param1=something&param2=something#anchor);
Run Code Online (Sandbox Code Playgroud)

我也试过这个:

Response.Redirect(/Script.aspx?param1=something&param2=something&#anchor);

无论哪种情况,删除Anchor都可以解决问题.

Chrome和Firefox中会出现此问题.Firebug报告请求完成并显示响应字符串,所有内容看起来应该是井号,除了浏览器之外的所有内容都会挂起.挂起我的意思是将光标显示为进度\做某种游标类型.

luk*_*fer 7

这是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(...)
Run Code Online (Sandbox Code Playgroud)

或者您可以将其作为附加查询字符串参数传递,并使用javascript附加锚点:

//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>");
}
Run Code Online (Sandbox Code Playgroud)