MVC controller is being called twice

心已入冬 提交于 2019-11-27 01:17:50

Is there any other markup that could be accidentally referencing the page? Script references, image references, css references, all could be mistakenly pointed at '.' or the current page.

10 hours chasing that bug in a Java Spring Maven project.

First on SELECT I thought Hibernate was just logging twice, but then with INSERT I thought requests were called twice. Stepping throught the code I discovered controller was called twice...

Tried all possible Spring configuration, thinking the context was loaded twice or a bean was instantiate twice...

In despair, rebuilded the project piece by piece to finally add a fragment of HTML and kaboom bug's back.

<img alt="" src="#" />

The sharp sign was guilty, reloading the URL. I know the topic is old, but I summarized my searchs with the words I used to look for in vain on Internet to find an answer to the same issue! Could help others...

You can step through the code in your view. Step through and see where the second call comes from.

While debugging I found out that a Partial View causes the the Controller to be called a second time. It sucks, but I don't see a work around that one.

there should be a html markeup that is not working properly. please check all img tage. also check

<link rel="icon" href="favicon.ico" type="image/x-icon" />

Try changing the int? id to int id. It's matching the route the 2nd time because you're calling the index again with a null id.

You can also try changing your route to this.

routes.MapRoute( 
    "Play", // Route name 
    "Play/{id}", // URL with parameters 
    new { controller = "Play", action = "Index" , id = "" } // Parameter defaults 
);

Something very stupid I did...had 2 Forms on a page with one button on each form. I had added script to submit a specific form based on the button clicked but, since the default action of a button on a form is to do a submit, it called my Controller Action twice. :^(

    $('#SaveButton').click(function (event) {
        $("#AttendanceDetailForm").submit();
    });

I was also facing the same issue. after thoroughly checking my project , i found none such empty reference case. Then i found out that it is caused by FireBug. Disabling firebug or using other browser which has not firebug installed solved the problem.

I had this same issue and verified all the possible suggestions but no luck then I noticed following JS warning message in my Console.

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.

xmlHttp = new XMLHttpRequest();

I corrected this and it works for me.

Take care all your js errors and warning messages.

This may help to someone.

My issue was resolved by making sure I wasn't double referencing my JavaScript files, which I was.

This was causing the action to be hit twice when I clicked the link.

I know the above has been said before but I thought I would point out that it's worth checking to see if your files are being loaded twice, especially if you're using Partial Views.

I noticed that in one of my Partial Views I was telling it to use the main layout page which contained the scripts that were responsible for what happened when I clicked on the links. So I fixed this by just setting layout = null; since it's a partial view anyway and is already being loaded inside of the main layout.

In my case, I was using a Partial View (so no Form tags) and using an on click handler in JQuery to call a method in the controller via Ajax. But I had declared the button the handler was attached to as type Submit. I forgot to pass in e to my handler function so as to call e.PreventDefault()!! So the Controller method was being called twice - once from an ajax call and once for Submit. The 2nd time around the parameters were null. This caused me so much grief. Such a small thing. So small that it was easily overlooked. Hope this helps someone else.

In my case it was incorrectly configured remote script Yandex.metrika (Google analytics analog). This script was presented on each page, so any controller and any action was called twice. Check your Ya.metrika settings for more details.

This is an old question and some answers are still useful. That is why I am adding a new answer, hoping that will help someone else. For me, I have an app that redirects users back and forth between my domains. Due to some of my recent HttpCookie related work, I had added below line of code:

httpCookieObject.SameSite = SameSiteMode.Strict;

Turns out the SameSiteMode.Strict causes issues when it comes to cross-origin authentication schemes. This is Microsoft documentation about it: https://docs.microsoft.com/en-us/aspnet/core/security/authentication/cookie?view=aspnetcore-2.2

To strictly enforce a same-site policy of SameSiteMode.Strict, set the MinimumSameSitePolicy. Although this setting breaks OAuth2 and other cross-origin authentication schemes, it elevates the level of cookie security for other types of apps that don't rely on cross-origin request processing.

So, my solution was that I do not use the SameSiteMode.Strict policy and I am good to go.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!