3

I have a MVC Action Method which is being hit twice if the request takes time to complete. On checking chrome console network log and fiddler, the request is being sent only once from the JS.

In the diagram below, I'm facing this issue with Approach 1. However, it works fine if I do it with Approach 2.

What’s happening with Approach 1 (failure):

  1. JS hits MVC Action with window.location.href.
  2. Ideal scenario as per code
    • MVC Action (Time taking process)
      • Calls Service Method 1
      • Calls Service Method 2
      • Redirects to the confirmation/final page.
  3. What actually happens
    • MVC Action (Time taking process)
      • Calls Service Method 1
      • Calls Service Method 2
      • Waits for approximately 4 minutes (Unable to determine the reason)
      • Calls MVC Action again
      • Validation fails and response is redirected to error page since the action method is being called twice with same parameters.

What’s happening with Approach 2 (success):

  1. Split the MVC Action calls into two, 1 and 2
  2. JS hits MVC Action 1 using Ajax
  3. MVC Action 1
    • Calls Service Method 1
    • Responds with the URL to MVC Action 2
    • JS receives MVC Action 1 response and calls MVC Action 2 using window.location.href
  4. MVC Action 2
    • Calls Service Method 2
    • Redirects to the confirmation/final page successfully.

Even though I’ve found a probable solution with Approach 2, I can’t understand why Approach 1 fails.

The main points that bother me are:

  1. Why is the MVC Action being called twice even if the process takes long time to complete? Couldn’t it just have timed-out?
  2. Why there is a gap of approximately 4 minutes before the second call happens?

Alternate Flows

Edit: I've tried it on Chrome 45.0.2454.101 m, Firefox 40.0.3, IE (7, 8, 9, 10) without any success.

ASP.NET MVC 2.0
.NET 4.0

Note: I've looked at other similar problems (google/SO) but it didn't help me.

5
  • Have you tried with different browsers? It could be (no evidence provided) that the browser is getting a timeout so trying again, but not telling you in the console. (though this does sound unlikely, just speculation). It's unlikely the top3 browsers would work the same in this regard (so if it does it on all, this isn't the case). Commented Oct 8, 2015 at 10:44
  • 1
    Generally, if you have a long-running process, you don't want to use location.href. Showing a page then a 'loading' gif is much better for the user. Why not combine Approach2 call with Approach1 controller-action? ie make an ajax call to Action which calls Service1+Service2. Commented Oct 8, 2015 at 10:45
  • This looks like a good scenario for signalr - especially if your services can give additional feedback. Commented Oct 8, 2015 at 10:48
  • @freedomn-m thanks for your suggestion on a third approach. I'll definitely try out that approach as well. As for browsers, i've tried on all of them, including different OS. Commented Oct 8, 2015 at 10:55
  • Can you post your AJAX or JavaScript? Commented Aug 7, 2016 at 23:34

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.