问题
I have a controller that is being called twice from an ActionLink call.
My home page has a link, that when clicked calls the Index method on the Play controller. An id of 100 is passed into the method. I think this is what is causing the issue. More on this below.
Here are some code snippets:
Home page:
<%= Html.ActionLink("Click Me", "Index", "Play", new { id = 100 }, null) %>
Play Controller:
public ActionResult Index(int? id)
{
var settings = new Dictionary<string, string>();
settings.Add("Id", id.ToString());
ViewData["InitParams"] = settings.ToInitParams();
return View();
}
Play view:
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
(html <head> omitted for brevity)
<body>
<form id="form1" runat="server" style="height:100%">
Hello
</form>
</body>
If I get rid of the parameter to the Index method, everything is fine. If I leave the parameter in place, then the Index method is called with 100 as the id. After returning the View, the method is called a second time with a parameter of null.
I can’t seem to figure out what is triggering the second call.
My first thought was to add a specific route like this:
routes.MapRoute(
"Play", // Route name
"Play/{id}", // URL with parameters
new {controller = "Play", action = "Index"} // Parameter defaults
);
This had no effect other than making a prettier looking link.
I am not sure where to go from here.
回答1:
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.
回答2:
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...
回答3:
You can step through the code in your view. Step through and see where the second call comes from.
回答4:
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.
回答5:
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" />
回答6:
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.
回答7:
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
);
回答8:
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();
});
回答9:
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.
回答10:
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.
回答11:
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.
回答12:
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.
回答13:
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.
回答14:
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.
来源:https://stackoverflow.com/questions/2751138/mvc-controller-is-being-called-twice