Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
share|improve this question
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 Answers 2

up vote 12 down vote accepted

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.

share|improve this answer
    
+1 for sharing relevant experience and putting the answer in context –  Tom Resing Jun 26 '12 at 18:59

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.

share|improve this answer
    
+1 for the reference link and the detail on the flags. –  Tom Resing Jun 26 '12 at 18:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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