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.
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
);
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.
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();
});
It was old, but some one will need it. My problem is style="background-image: url(../)
. Let's find html, css code about this style
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.
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://learn.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.
I was chasing my problem and found it in here:
Had <div style="width: 200px; height: 200px; background-image: url('\Asd\image.jpg')"></div>
and problem made \
character so inside server side code i replaced all \
with /
and it is working as charm.
I also had the same problem. my problem was because of an add-on I installed in the browser.
Check out the add-ons in your browser.
I had same script reference in my View and the _Layout.cshtml. Removing the reference in either place resolved the issue
@Scripts.Render("~/bundles/jqueryval")
I'm using ASP.NET MVC and got the same problem. In my _Layout.cshtml, there was a deprecated tag "bgsound". The problem got solved by replacing "bgsound" with "audio". I think some deprecated tags may cause the same problem.
As far as I know, this issue can have three main reasons.
You called the controller function twice from the client. That means: New Request => New Scope => New Controller Instance. So a debug mark in the controllers constructor should hint on a second call.
You're calling twice from a middleware.
class Middleware: IMiddleware {
public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
await next(context); //call #1
await next(context); //call #2
//This is tricky... base calls might also call the controller function, and those are easily overlooked!
await base.InvokeAsync(HttpContext context, RequestDelegate next);
}
}
- You're calling twice from the controller itself:
class SomeController: Controller {
public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
await next();//call #1
await next();//call #2
//And again... tricky... base calls might also call the controller function, and those are easily overlooked!
await base.OnActionExecutionAsync(context, next);
}
}
However, there might be more reasons to this issue, that i'm just not thinking about at the moment. The easiest way to indicate second calls within the same request, is to keep "stepping into" at the end of the first call. You will automatically end up at the second call at some point.
精彩评论