0

I'm trying to implement Redirect After Post for the first time in ASP.NET. Assuming my business objects may take several seconds to a minute to complete, in what order, and what syntax do I use?

For example:

  1. User POST's

  2. Server issues Server.Transfer or Response.Redirect

  3. Server does something that takes a minute or two Thread.Sleep

What is the best way to handle this type of situation?

makerofthings7
  • 60,103
  • 53
  • 215
  • 448
  • Do you want the user to know the result of his submission, in case of failures? – Holystream Aug 24 '10 at 16:42
  • possible duplicate of [Server.Transfer Vs. Response.Redirect](http://stackoverflow.com/questions/224569/server-transfer-vs-response-redirect) – Nathan Taylor Aug 24 '10 at 16:42
  • @Nathan Taylor: Not a duplicate... although that was helpful, it seems I need Response.Redirect in the "Redirect After Post" pattern, but I'm not sure of the steps? Should I not invert #2 with #3? I'm just assuming this is the correct sequence – makerofthings7 Aug 24 '10 at 17:20

2 Answers2

2

In this case, it is probably best to just stick with Response.Redirect() so that the user's client is issued a redirect, rather than Server.Transfer() which performs a purely server-side redirect to a different context.

Regarding the process which requires the user to wait, you may want to use some sort of asynchronous implementation where the time-consuming operation is placed in a background thread; meanwhile the user, instead of waiting on a blank loading screen, is given Response.Redirect() to a "Processing" page that polls the server for completion of the current operation and updates the user. For added polish, consider implementing something like Facebook's image uploader which overlays a progress bar in the corner of the screen while the user continues normal use of the website.

Nathan Taylor
  • 24,423
  • 19
  • 99
  • 156
  • Do you have any tips on how I can add that polish? Does javascript just poll a WCF service (or equiv.)? – makerofthings7 Aug 24 '10 at 17:46
  • 1
    Exactly, the server would have some sort of web method which retrieves the progress of the currently executing process and returns it to JavaScript. From there JavaScript would simply update its progress bar accordingly. You can use a JavaScript setInterval() to instruct client to poll the server every few seconds for the current progress. This answer to another question has an example of [how to implement a polling process with JavaScript](http://stackoverflow.com/questions/3526955/displaying-post-data-with-jquery/3527018#3527018). – Nathan Taylor Aug 24 '10 at 17:54
  • Lastly, would it make sense to put the bizlogic after the redirect to avoid double-submit? There seems to be a chance it may occur... – makerofthings7 Aug 24 '10 at 20:21
  • @Maker The location of the business logic (before or after redirect) won't prevent a double submit because either way, when the user re-POSTs the data it will still invoke the same erver-side logic. [This answer](http://stackoverflow.com/questions/1498269/asp-net-double-click-problem/1498290#1498290) has a good explanation of how to prevent double submissions should they be a valid concern in your scenario. – Nathan Taylor Aug 26 '10 at 01:22
1

From what I can see you need to do:

  1. User POSTs form to server script
  2. Server does something that takes a minute or two using Thread.Sleep
  3. Server issues a Response.Redirect

However, the obvious drawbacks here are that the user is kept waiting whilst the server does some work (two minutes is a long delay period, they may well assume that something has gone wrong), and possibly you might hit a HTTP request time out on the browser.

In terms of code, it's pretty straightforward:

  1. This is a standard <FORM METHOD="POST"> code
  2. Do whatever you need to do, can't see why you would need Thread.Sleep at the moment; wouldn't you want to redirect to happen as soon as your server side processing has completed?
  3. Call Response.Redirect("mypage.aspx") to perform the GET

Does that help?

Sam Huggill
  • 3,106
  • 3
  • 29
  • 34