-2

I'm working on ASP.NET MVC3 with C#.

What is difference between Response.Redirect("http://www.google.com"); and Response.Write("REDIRECT=http://www.google.com");?

halfer
  • 19,824
  • 17
  • 99
  • 186
Dharmik Bhandari
  • 1,702
  • 5
  • 29
  • 60

4 Answers4

6

The difference is that the first will replace the response with a redirection page and end the execution, while the second will just write the text to the response stream and continue with creating the rest of the page.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    Good answer, +1. In addition to that I would say that none of them should be used in ASP.NET MVC. – Darin Dimitrov Jun 14 '12 at 06:16
  • Why you say that none of them should be used in ASP.net MVC? Any specific reason behind this? – Krunal Jun 14 '12 at 18:00
  • 1
    @Krunal: Because in MVC you should return a `RedirectResult` from the method in your controller to do a redirection. – Guffa Jun 14 '12 at 18:15
0

Response.Redirect() sets an HTTP 302 header along with the URL to be redirected to.

Response.Write("REDIRECT=http://www.google.com"); will write that string to the response body, as in that redirect text would be appended to the HTML of your web page.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
0

This will create the correct full HTTP Header for you:

Response.Redirect("http://www.google.com");

You have the ability to set or change some paramters for the HTTP Header.

HttpResponse Class

e.g set HTTP Status Code 404 or 500 or in your case 302 for redirect.

e.g set the HTTP Mime-type for jpg

Will write into the Body in your response..like a string output

Response.Write("REDIRECT=http://www.google.com");
theforce
  • 1,505
  • 1
  • 11
  • 13
0

The methods in question are quite self explanatory :)

Response.Redirect("http://www.google.com");

The Redirect will redirect you to another page, in the case it will take you to Google's home page.

Response.Write("REDIRECT=http://www.google.com");

The Write method will write a string of text to the web page. In this case it will write the text "REDIRECT=http://www.google.com" to your web page.

Play around with these 2 methods in your web project and see what happens.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234