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
1
  • 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, 2012 at 19:00

2 Answers 2

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.

1
  • +1 for sharing relevant experience and putting the answer in context
    – Tom Resing
    Jun 26, 2012 at 18:59
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.

1
  • +1 for the reference link and the detail on the flags.
    – Tom Resing
    Jun 26, 2012 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.