3

I am using server.transfer() method in my asp.net application to redirect the response. But I am running into the problem that it sets the previous page url (from where the original request for page was generated) at the browser url bar. I want to change the url in the browser. is it even possible??

I looked into it and i know that the Request has a url property but its read only. does any one know a way to change the url in the request?

svick
  • 236,525
  • 50
  • 385
  • 514
Arslan Faisal
  • 187
  • 1
  • 1
  • 5
  • 1
    Why are you using Server.Transfer? Is there a reason you wern't using Response.Redirect? See http://www.csharpfriends.com/articles/getarticle.aspx?articleid=15 for some info. – Nick Bork Feb 17 '12 at 14:58

4 Answers4

4

Use Response.Redirect(); instead of server.transfer(); and it redirects in the browser.

If you can't do taht, you could use pushState (at least where it's aviable) to change the URL, but it seems a bit of a overkill...

The best way is clearly to change

  server.transfer();

to

  Response.Redirect();

EDIT

as you want to have the maximum performance, you could should use Response.Redirect with two parameters, and set the second to true.

so instead of

  server.transfer(url);

you should have

to

  Response.Redirect(url, true);

That causes the current request to abort and force a instant redirect.

ramsesoriginal
  • 1,860
  • 1
  • 13
  • 16
  • Thanks a lot for your response but actually i want to save the extra round trip to improve the performance that's why i want to use server.transfer() – Arslan Faisal Feb 17 '12 at 15:01
  • `Response.Redirect()` sends only a `HTTP` header, and is really fast. creating a JavaScript taht uses pushState (http://www.w3.org/TR/html5/history.html) (like I said in my answer) would be possible (at least in modern browsers), but requesting the javascript, modifying the history etc is way more inefficient then a simple HTTP request. You'd have to have huge cookies and incredibly slow conenctions to notice have a *significant* delay with `Response.Redirect();` – ramsesoriginal Feb 17 '12 at 15:05
  • FWIW http://msdn.microsoft.com/en-us/library/t9dwyts4.aspx says that setting true as the second parameter is superfluous since that is the default if it is omitted. – Chris Feb 17 '12 at 15:17
  • I know that it should be like that, but I also had some cases where it actually kept on processing the page, and with explicitly setting the parameter to true it worked as expected.. – ramsesoriginal Feb 17 '12 at 15:20
  • The performance impact of `Server.Transfer` over `Response.Redirect` is minimal, and indeed in some cases the former is slower (if you redirect to a resource that's already in the cache then only the very small redirect need be sent at all). The reason for favouring one over the other is nothing to do with performance and everything to do with whether it's just a convenient way for the server to have different ASP.NET pages handle the same URI in different cases (`Server.Transfer`) or the user should indeed be sent to another page (`Response.Redirect`). – Jon Hanna Oct 23 '14 at 00:49
0

Description

You can't change the Url of the Current request because it is already running.

I think you want to do a redirect.

The Redirect method causes the browser to redirect the client to a different URL.

Sample

Response.Redirect("<theNewUrl>");

Update

If you want to change the Url in the Browsers Address Bar without doing a requestion read this:

Can I change the URL string in the address bar using javascript

More Information

Community
  • 1
  • 1
dknaack
  • 60,192
  • 27
  • 155
  • 202
  • Thanks for your response.but No i was using Response.Redirect() but to improve the performance i shifted to server.transfer() and since it transfers control from one page to another on the server without letting the client know about it. so the url in the browser don't get change but i want to be able to rewrite this url. – Arslan Faisal Feb 17 '12 at 15:00
  • Then you need to do this using javascript. Check out the link in my answer. – dknaack Feb 17 '12 at 15:03
0

Server.Transfer() is just changeing which content you send back.

Response.Redirect() is what you need to tell the browser to go to a new page

You cannot change the URL of a request - it would make no sense, the URL is what your client (the browser) has asked for.

dice
  • 2,820
  • 1
  • 23
  • 34
0

No, you cannot change the URL in the browser like that. That would be a pretty massive security hole if you were able to do that. http://EvilDomain.com would be able to seamlessly masquerade as http://YourOnlineBank.com and no one would be any the wiser.

48klocs
  • 6,073
  • 3
  • 27
  • 34