23

I'm using SharePoint 2010. I have webparts, then when OK/Save/Cancel etc is clicked, it will redirect a user to the relevant page.

Over the past few months of writing different WebParts I have checked over my code and see that I'm using different ways of redirecting the user. My question is, which is the correct one to use and why?

SPUtility.Redirect
Page.Response.Redirect
Context.Reponse.Redirect
Cann0nF0dder
  • 1,813
  • 3
  • 20
  • 32
  • 1
    I like that you asked this kind of question. Sometimes subtle differences like this can be the hardest thing to find concrete information on. – Tom Resing Jun 26 '12 at 19:00

2 Answers2

19

I usually use SPUtility.Redirect when working with Application Pages or pages that need to potentially look at the source querystring or go back to settings.aspx.

If I am doing a redirect other times (usually in an HttpModule), I will use the Context.Response.Redirect or HttpContext.Current.Response.Redirect. The Page.Response.Redirect is just exposing the same object as Context.Response.Redirect, so they are essentially the same.

Steve Lineberry
  • 7,960
  • 22
  • 24
11

The difference between SPUtility.Redirect and Page.Response.Redirect is that SPUtility.Redirect provides many more options while redirecting. SPUtility.Redirect takes in the SPRedirectFlags enumeration as one of the parameters which gives us more control over the redirection. With it we can even keep the reference of the previous page.

More details on the SPUtility.Redirect over here: https://www.nothingbutsharepoint.com/sites/devwiki/SP2007Dev/Pages/How%20to%20use%20SPUtility.Redirect.aspx

And as Steve said, Page.Response.Redirect and Context.Response.Redirect are the same object.

RJ Cuthbertson
  • 8,342
  • 6
  • 38
  • 76
Vardhaman Deshpande
  • 10,462
  • 1
  • 26
  • 52