ASP.NET MVC Action被称为两次

时间:2022-11-30 17:33:25

I have a specific controller action that is being called twice, which aside from being weird is causing problems with a singleton portion of my application (not really a problem, it just brought the double call to my attention).

我有一个特定的控制器动作被调用两次,除了奇怪之外导致我的应用程序的单个部分出现问题(不是真正的问题,它只是引起我的双重调用)。

Any idea why a controller action would be executed twice every time?

知道为什么控制器动作每次都要执行两次吗?

9 个解决方案

#1


23  

Not returning false or preventing the default action on the event in a JavaScript click handler on a link that makes the call via AJAX. In this case, it will also take the default action of the link (i.e., a non-AJAX post to the same URL).

在通过AJAX进行调用的链接上的JavaScript单击处理程序中,不返回false或阻止对事件的默认操作。在这种情况下,它还将采用链接的默认操作(即,非AJAX帖子到同一URL)。

Ex.

防爆。

<%= Html.ActionLink( "action", "controller" ) %>


$(function() {
   $('a').on('click', function(e) {
      // solve with "e.preventDefault();" here
      var href = $(this).attr('href');
      $.get(href, function() { ... });
      // or solve with "return false;" here to prevent double request
   });
}):

#2


7  

I had a similar issue. The issue was caused by this line in my _Layout.cshtml file:

我有一个类似的问题。该问题是由我的_Layout.cshtml文件中的这一行引起的:

<link rel="shortcut icon" href="">

After removing the above line, my actions are called once.

删除上面的行后,我的操作被调用一次。

#3


5  

This is late as well but perhaps this will help someone.

这也是迟到的,但也许这会对某人有所帮助。

My issue was similar (controller method would fire once on initial load and then would fire again right away asynchronously).

我的问题是类似的(控制器方法会在初始加载时触发一次,然后会异步地再次触发)。

I was able to inspect the HttpContext.request object and determined that the second request was due to Visual Studio's "Browser Link" feature.

我能够检查HttpContext.request对象,并确定第二个请求是由Visual Studio的“浏览器链接”功能引起的。

I disabled Browser Link and the mystery ajax call to my controller method no longer happens.

我禁用了浏览器链接和神秘的ajax调用我的控制器方法不再发生。

How to disable Browser Link

如何禁用浏览器链接

#4


4  

Very late but I found that having

很晚但我发现有

<img id="myLogo" src="#"/> 

caused this to make it do the call again. Hope this helps someone, the reason I am not sure.

这导致它再次进行通话。希望这有助于某人,我不确定的原因。

Found solution here. http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html

找到解决方案。 http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html

#5


1  

In my specific case, it was the loading of jquery.unobtrusive-ajax.min.js file for twice.
This was causing the $(document).ready(function() { }); function to be executed two times.

在我的具体情况下,它是两次加载jquery.unobtrusive-ajax.min.js文件。这导致了$(document).ready(function(){});功能要执行两次。

Hope this is helpful.

希望这有用。

#6


0  

Remove null able argument from view

从视图中删除null able参数

public ActionResult Edit(int? id)

to

public ActionResult Edit(int id)

#7


0  

My problem was that I accidentally included the recaptcha/api.js file form google twice. One in the BaseLayout and one in the view.

我的问题是我不小心将两次recaptcha / api.js文件格式包含在谷歌中。一个在BaseLayout中,另一个在视图中。

#8


0  

Using kendo grid this issue happened. use grid option AutoBind(false)..

使用kendo网格这个问题发生了。使用网格选项AutoBind(false)..

#9


0  

I had a similar issue. The issue was caused by this line in parital view.cshtml file:

我有一个类似的问题。该问题是由parital view.cshtml文件中的这一行引起的:

after removing it is fixed

删除后它是固定的

#1


23  

Not returning false or preventing the default action on the event in a JavaScript click handler on a link that makes the call via AJAX. In this case, it will also take the default action of the link (i.e., a non-AJAX post to the same URL).

在通过AJAX进行调用的链接上的JavaScript单击处理程序中,不返回false或阻止对事件的默认操作。在这种情况下,它还将采用链接的默认操作(即,非AJAX帖子到同一URL)。

Ex.

防爆。

<%= Html.ActionLink( "action", "controller" ) %>


$(function() {
   $('a').on('click', function(e) {
      // solve with "e.preventDefault();" here
      var href = $(this).attr('href');
      $.get(href, function() { ... });
      // or solve with "return false;" here to prevent double request
   });
}):

#2


7  

I had a similar issue. The issue was caused by this line in my _Layout.cshtml file:

我有一个类似的问题。该问题是由我的_Layout.cshtml文件中的这一行引起的:

<link rel="shortcut icon" href="">

After removing the above line, my actions are called once.

删除上面的行后,我的操作被调用一次。

#3


5  

This is late as well but perhaps this will help someone.

这也是迟到的,但也许这会对某人有所帮助。

My issue was similar (controller method would fire once on initial load and then would fire again right away asynchronously).

我的问题是类似的(控制器方法会在初始加载时触发一次,然后会异步地再次触发)。

I was able to inspect the HttpContext.request object and determined that the second request was due to Visual Studio's "Browser Link" feature.

我能够检查HttpContext.request对象,并确定第二个请求是由Visual Studio的“浏览器链接”功能引起的。

I disabled Browser Link and the mystery ajax call to my controller method no longer happens.

我禁用了浏览器链接和神秘的ajax调用我的控制器方法不再发生。

How to disable Browser Link

如何禁用浏览器链接

#4


4  

Very late but I found that having

很晚但我发现有

<img id="myLogo" src="#"/> 

caused this to make it do the call again. Hope this helps someone, the reason I am not sure.

这导致它再次进行通话。希望这有助于某人,我不确定的原因。

Found solution here. http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html

找到解决方案。 http://skonakanchi.blogspot.com/2011/09/action-method-calling-twice-in-aspnet.html

#5


1  

In my specific case, it was the loading of jquery.unobtrusive-ajax.min.js file for twice.
This was causing the $(document).ready(function() { }); function to be executed two times.

在我的具体情况下,它是两次加载jquery.unobtrusive-ajax.min.js文件。这导致了$(document).ready(function(){});功能要执行两次。

Hope this is helpful.

希望这有用。

#6


0  

Remove null able argument from view

从视图中删除null able参数

public ActionResult Edit(int? id)

to

public ActionResult Edit(int id)

#7


0  

My problem was that I accidentally included the recaptcha/api.js file form google twice. One in the BaseLayout and one in the view.

我的问题是我不小心将两次recaptcha / api.js文件格式包含在谷歌中。一个在BaseLayout中,另一个在视图中。

#8


0  

Using kendo grid this issue happened. use grid option AutoBind(false)..

使用kendo网格这个问题发生了。使用网格选项AutoBind(false)..

#9


0  

I had a similar issue. The issue was caused by this line in parital view.cshtml file:

我有一个类似的问题。该问题是由parital view.cshtml文件中的这一行引起的:

after removing it is fixed

删除后它是固定的