views:

34

answers:

2

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?

+1  A: 

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
@Sam Huggil Thread.Sleep is just simulating a backend process that takes some time to complete..
MakerOfThings7
Yes, thank you... seems that the BizLogic is always run before the Redirect in any case.
MakerOfThings7
+1  A: 

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
Do you have any tips on how I can add that polish? Does javascript just poll a WCF service (or equiv.)?
MakerOfThings7
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
@Nathan Taylor 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
@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