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!

4

2 回答 2

1

如果您在网络选项卡上的控制台窗口中看到您的请求,则有两种请求:-

1:文档类型 2:XmlHttpRequest

文档类型请求来自视图,它是动作链接,其中提到了动作方法名称,但如果您看到另一个请求,则该请求来自 Javascript 环境。

请参阅 XMLHttpRequest 的官方定义:- XMLHttpRequest (XHR) 是一种对象形式的 API,其方法在 Web 浏览器和 Web 服务器之间传输数据。该对象由浏览器的 JavaScript 环境提供。

请验证您的 JS 部分。

于 2019-10-16T06:33:03.840 回答
0

感谢大家对此的关注。我有 site.js,我在其中保存所有脚本代码和 ajax 调用代码。有一个 ajax 调用旨在在特定页面上调用,但由于代码不正确,该 ajax 调用在每个其他页面加载后触发。因此,我更正了该 ajax 调用的代码,现在我没有看到对控制器的任何重复调用,一切正常。感谢大家。

于 2019-10-17T04:32:40.997 回答