views:

34

answers:

2

Hi-

What I am trying to do is render a View in an MVC site informing the user to not refresh their browser while server side processing is taking place. This could be a long running task, and if they hit refresh it will send the request again, thus sending them to the error screen when the process was actually successful. I was going to do this in JS, either with JQuery $.ajax(...) or with a simple $(document).ready(function() { window.location = ... }); but I was hoping there was a way to do it in MVC, thus giving me more control over the HttpResponseCode which is returned to the client calling. Is there a way to do this?

I was thinking along the lines of

    public ActionResult LoadingAsync(string UserKey, string userInf)
    {
        AsyncManager.OutstandingOperations.Increment();

        CallProcess(...)

        return View();
    }

then have a Completed Action perform the redirect

   public ActionResult LoadingCompleted()
    {
        LongRunningProcess();
        return Redirect("http://yourdone.com");
    }

or just have something that Renders inside the view that will perform the Redirect from inside the View

    <% Html.RenderAction("Process"); %><!--This won't actually redirect-->

Any ideas?

A: 

One simple solution would be to fire off your background process, then display a page which (1) asks the user not to refresh, (2) polls the server every couple of seconds via Javascript to determine if the background process is complete, and (3) redirects upon determining that it is complete.

paulbonner
I would still be using a js redirect...
beatn1ck
A: 

There is no way to achieve this without invoking some kind of Javascript on the client.

When you return a response to the user, you either return a page to display with an HTTP code of 200 (OK), or an instruction to redirect with an HTTP code of 301 (Moved Permanently) or 307 (Moved Temporarily) and a URL to redirect to.

You have to choose either of these return values and cannot return both.

The simplest solution is to use Javascript to redirect the user from your "please wait" page to the destination once it determines the background process has completed.

Programming Hero
Yeah, I think you're right. I was hoping for some MVC magic. I'll just do a window.location in which I can set my 303 on the browser 'GET' ~thanks
beatn1ck