Search code examples
c#asp.net-mvcasp.net-mvc-4asp.net-mvc-5actionresult

ActionResult method in .NET MVC gets loaded twice


I was working on something today on my project and I noticed something very strange that occurs on all of my Index pages inside a project. For example I have the action which looks like this:

   public ActionResult Index(string id=null, string IsOld=null)
   {

        string id = null;
        string IsOld = null;
        return View();  
   }

Without any indicative reason, this method calls twice. For example if I put the break point on the:

   string id = null;

I will see the compiler stop twice on that line of code while the page loads... On some of the pages this doesn't happen... I have checked for the page and scripts but nothing that would indicate to call this same method twice upon load... It is very strange, and I'm clueless as to why this happens?

Did anyone experience this kind of issue before, can someone help me out?

P.S. I have also tried disabling all scripts on this page so that I exclude possibility that some javascript is doing post or get to call the method twice... Even with disabling all the scripts, I still get the double call...


Solution

  • I have seen this behaviour before, especially on the default url, it can be a few things:

    • Empty image tags
    • Empty src tags on a script tag
    • Empty link on a css tag
    • Google adwords caused it once - they had a bug last year IIRC
    • Nested forms in html
    • Forgotten meta refresh head tag
    • Doubly bound JS click handler
    • Html.RenderAction in a nested view - out of sight, out of mind etc

    It will a good idea to start the network inspector in your browser of choice. If you can see the double call being initiated in the browser, that will tell you if it is client side or if something else is inside your razor pages is causing it.

    Happy hunting