Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.Redirect - Problem Sending Redirect with Hash if Current page had URL of destination URL with Hash

I am using ASP.NET Webforms C# 3.5 and utilizing Response.Redirect to redirect the page after an event call.

If I submit a request the first request works fine. I am using Response.Redirect currently. I have tried using Server.Transfer and passing True and False as the second parameter to the Redirect function as well as attempting to turn off SmartNavigation.

IF I click my submit button the page does what it's supposed to and the anchor forces the page to the correct location. If I then click submit again the same URL is returned and the browser doesn't do anything but sit and "hang." If I remove the hashed portion from my URL the redirect works fine.

The problem only seems to occur if the request was redirected to the current URL with an anchor and a new redirect occurs with the anchor (hash symbol)

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

I've also tried this:

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

In either case removing the Anchor fixes the problem.

This problem occurs in Chrome and Firefox. Firebug reports the request completing and shows the response string and everything looks like it should with pound sign and all but the browser just hangs. By hang I mean shows the cursor as the progress\doing something cursor type.

like image 648
Joshua Enfield Avatar asked Feb 25 '23 02:02

Joshua Enfield


1 Answers

This is a common problem with Response.Redirect.

You can either set

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(...)

Or you can pass it as an additional query string parameter and use javascript to append the 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>");
}
like image 142
lukiffer Avatar answered Mar 16 '23 01:03

lukiffer