简体   繁体   中英

method is called 2 or 3 times in each request .NET MVC

I'm facing an issue in a project that I'm currently working on, I'm using ASP.NET MVC.

The scenario as follow:
- I have a login page (Username and Password).
- Whenever I navigate to localhost:5588/login, the below action method will be called 2 or 3 times (I'm using a Break Point inside this method to catch the call).

public ActionResult Login()
{
     return View();
}

The question is, why this method is called 2 - 3 times whenever I enter the login page ?

PS #1: Not only the Login page is being called 2-3 times, also each Action method have the same issue.
PS: #2: I'm using the below route:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
        );       
}

UPDATE:
This issue happens only on Google Chrome!

Issue might happen, because browser can pre-load page, before you hitting Enter. In this thread posted solution, how you can understand that it is pre-load request: HTTP header to detect a preload request by Google Chrome

显然,有些东西将您的操作方法称为3次,强烈建议您打开chrome开发人员工具,导航到网络并检查流量,如果它们是XHR请求,您也可以跟踪它们的来源,否则就处于黑暗中。

I once had this issue and found I had something like this

src="#"

in my image tag or check for any other markup that could be accidentally referencing the page like Script references, image references, css reference etc

This is the correct answer and this actually solved the issue. [Special Thanks to my friend Mohammad Aldayem for the great finding].

Based on an issue posted on Chromium Issue Tracker ( https://bugs.chromium.org ), the issue was in the " favicon.ico " in the _Layout.cshtml page. Because Chrome requests favicons on every request on pages that don't have a favicon.

And here are the links for this issue while using Google Chrome:

Link #1: https://bugs.chromium.org/p/chromium/issues/detail?id=64810
Link #2: https://bugs.chromium.org/p/chromium/issues/detail?id=39402

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM