User13839404
User13839404

Reputation: 1813

What is the alternative to Response.Redirect() asp.net?

Hi One of the tips in "website performance tips" in various blogs says "Avoid Redirects". In my case, I am using Response.Redirect for the same page. I am passing a querystring and displaying appropriate information to the user.

Response.Redirect("FinalPage.aspx?NextID=" + ID);

So in our business logic, i am reloading the same page with different information.

So how do i avoid redirect? Is there any other alternative? BTW, my aim is to gain some performance there.

Upvotes: 5

Views: 18115

Answers (5)

Harsh Baid
Harsh Baid

Reputation: 7249

I think if you want to redirect to same page then instead of doing Response.Redirect("FinalPage.aspx?NextID=" + ID); you could use NextID in ViewState also or Hidden Field so that you would not required to redirect SAME page and then check that hidden field or viewstate instead of checking QueryString

:D

Upvotes: 0

KeithS
KeithS

Reputation: 71573

The main "evil" if it could be called such is that redirects require an extra round trip; the client requests one page (usually the same page, specifying that a particular button was clicked), and the server responds saying "request this page instead", and the browser then complies, resulting in the server actually serving up the next page.

It's sometimes necessary to do this, however there are now much better ways to control navigation in a website. For instance, instead of a "form" button that causes a postback and redirect, you could use a LinkButton that will behave like a hyperlink, allowing the browser to request the new page directly. You could also use a MultiView that shows different ASCXs, and control navigation by view-flipping (however, understand that this can have its own performance implications, especially when using them in a nested fashion).

Upvotes: 0

Aliostad
Aliostad

Reputation: 81660

Redirect is the R in the PRG pattern which is an accepted pattern for processing posted requests. So it is definitely not evil.

However, there used to be a common interview question: "What is the difference between Server.Redirect() and Server.Transfer() and which one must be used?". People used to say Transfer because it did not involve a round-trip but web has changed so much since then. In those days you could not re-use the the common logic in the views unless you use Transfer or Redirect, but nowadays especially with ASP NET MVC there are tons of a ways to do that.

In your case, I am all for PRG and I believe redirect is semantically more correct. Also it prevents the form being re-submited if user clicks F5 or refresh.

Upvotes: 6

Mark Avenius
Mark Avenius

Reputation: 13947

You can also do a Server.Transfer, which does not require a new request to come in, thus lessening the load on the server. More information comparing the two is here.

In your case, you do want to do a Redirect because you are modifying the query string and changing something on the page, as opposed to shifting processing of the initial request to another page.

Upvotes: 0

Oded
Oded

Reputation: 499042

The recommendation is for unnecessary redirects.

Your case is different - you are passing in information to the page, this is not strictly the same thing as a regular redirect (i.e. a page that moved).

Upvotes: 3

Related Questions