0

I am working on an asp.net core application where I am loading a view on click of a hyperlink. The view loads fine but what strange thing happening here is whenever I click on the hyperlink, call to controller's action goes twice. I accidently found this issue when I put debug point on the action method and it got hit twice. Then I also checked the network traffic tab in the browser, there also I noticed 2 calls to controller action got recorded twice.

Markup of hyperlink in layout page:

<li><a asp-action="ApplyLeave" asp-controller="Home">Apply Leave</a></li>

Controller's action method:

[HttpGet]
public IActionResult ApplyLeave()
        {
            .......
            .......
            .......
            return View();
        }

ApplyLeave.cshtml

@{
    ViewData["Title"] = "Apply Leave";
}
<header class="page-header">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
                <h2 class="no-margin-bottom">
                    Apply Leave
                </h2>
            </div>
        </div>
    </div>
</header>

Screenshot of network traffic captured in browser: enter image description here

Kindly help me in resolving this issue, thanks in advance!

Simply Ged
  • 8,250
  • 11
  • 32
  • 40

2 Answers2

1

If you see your request in console window on network tab, there is two kind of request:-

1: Doc Type 2: XmlHttpRequest

Doc type request is coming from the View which is action link where action method name is mention, but if you see another request then, this request comes from Javascript environment.

Please see the official definition of XMLHttpRequest:- XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. The object is provided by the browser's JavaScript environment.

Please validate once your JS part.

Singh Aswal
  • 379
  • 2
  • 6
0

Thanks you all for your kind attention on this. I have site.js where I am keeping all the script code and code for ajax calls. There was an ajax call which was intended to be called on a particular page, but due to improper code, that ajax call was triggering after every other page load. So I corrected the code for that ajax call and now I am not seeing any duplicate call to controller, everything is working fine. Thanks everyone.