找到你要的答案

Q:MVC Action called twice

Q:MVC的行动称为两次

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?

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.

我有一个MVC操作方法这是被打了两次如果请求需要时间来完成。检查浏览器控制台网络日志和提琴手,请求被发送一次JS。

在下面的图表中,我将面对这个问题的方法1。然而,它的工作罚款,如果我用方法2。

方法1(失败)发生了什么?:

  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.

方法2(成功)发生了什么?:

  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.

即使我已经找到了一个可能的解决办法2,我不明白为什么方法1失败。

困扰我的要点是:

  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?

编辑:我想在Chrome 45.0.2454.101 m,40.0.3 Firefox,IE(7,8,9,10)没有成功。

ASP.NET MVC 2.0
.NET 4.0

注:我已经看了其他类似的问题(谷歌/ SO),但它没有帮助我。

javascript  asp.net-mvc  asp.net-mvc-2