c# - .Net Core 页面未缓存并单击浏览器后退按钮时出现 “ERR_CACHE_MISS” 错误页面

标签 c# .net asp.net-mvc asp.net-core browser-cache

我已经尝试解决这个问题好几天了,但运气不佳。

我们在数据表的 foreach 循环中有一个“详细信息”@Html.ActionLink。

然后在“Outcomes” Controller 中,我们有 HTTPGet 和 HTTPPost 操作来填充此数据表。

此处列出了结果 Controller 代码...... https://localhost:5003/Outcomes/ByProcedure

[HttpGet]
public IActionResult ByProcedure()
{
    …code to get result list

    return View(result);
}

[HttpPost]
public IActionResult ByProcedure(IFormCollection form, string submitForm)
{
    …after clicking submit button we take form input date pickers for start and end dates and pass in to get list

    return View(result);
}

当我单击详细信息链接时,它会转到不同的“报告” Controller 和操作,如下所示

<tr>
<td class="detailslink">@Html.ActionLink("Details", "ByProcedureDetailView", "Reports", new { @id = item.ProcTypeID }, null)</td>
</tr>

这是“Reports” Controller 中的操作结果,具有以下操作:https://localhost:5003/reports/ByProcedureDetailView/7

[HttpGet]
public IActionResult ByProcedureDetailView()
{
    code to load another datatable…

    return View(result);
}

数据表在此页面中填充良好。

但是,问题是如果我们单击此页面的后退按钮

https://localhost:5003/reports/ByProcedureDetailView/7

到此页面:

https://localhost:5003/Outcomes/ByProcedure

我们收到以下消息,并在单击后退按钮后在开发工具中看到该消息。

这种情况发生在 Chrome、Safari 和 IE 等中。

enter image description here

Confirm Form Resubmission
This webpage requires data that you entered earlier in order to be properly displayed. You can send this data again, but by doing so you will repeat any action this page previously performed.

*   Press the reload button to resubmit the data needed to load the page.

ERR_CACHE_MISS

我查遍了互联网,但没有发现任何有用的东西。

任何帮助将不胜感激。

最佳答案

This is the behavior of the browser.

如果提交页面是购物车结帐页面,并且服务器上没有进行转发检测,那么用户将被收取两次费用。这是一个非常常见的问题,浏览器必须检测到这一点并警告用户。

通常我们使用PRG pattern要解决这个问题,或者使用TempData

您可以引用this .

针对您的情况,可以结合以上两种方法来解决。

我创建了一个简单的演示,您可以引用如下:

    [HttpGet]
    public IActionResult ByProcedure()
    {
        var result = _context.Cobro.ToList();
        var value = TempData["Cobro"];
        if (value is string json)
        {
            result = JsonConvert.DeserializeObject<List<Cobro>>(json);
        }
        return View(result);
    }

    [HttpPost]
    public IActionResult ByProcedure(IFormCollection form, string submitForm)
    {
        var result = _context.Cobro.ToList().Where(x => x.Telefono == submitForm).ToList();
        TempData["Cobro"] = JsonConvert.SerializeObject(result);
        TempData["Name"] = submitForm;
        return RedirectToAction(nameof(ByProcedure));
    }

查看:

@model IEnumerable<WebApplication_core_mvc.Models.Cobro>
@{
    ViewData["Title"] = "ByProcedure";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h1>ByProcedure</h1>
<form asp-action="ByProcedure">
    <input id="Text1" type="text"  name="submitForm" value="@TempData["Name"]"/>
    <table class="table table-bordered">
        <tr>
            <th>Telefono</th>
            <th>Empresa</th>
            <th>Saldo</th>
        </tr>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Telefono</td>
                <td>@item.Empresa</td>
                <td>@item.Saldo</td>

                <td class="detailslink">@Html.ActionLink("Details", "ByProcedureDetailView", "Reports", new { @id = item.Presta }, null)</td>
            </tr>
        }
    </table>
    <input id="Submit1" type="submit" value="submit" />
</form>

测试结果如下: enter image description here

关于c# - .Net Core 页面未缓存并单击浏览器后退按钮时出现 “ERR_CACHE_MISS” 错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62493440/

相关文章:

c# - 使用值填充下拉列表

c# - 显示一个子虚拟机,然后在第一个关闭后显示另一个子虚拟机

c# - 在 StringTemplate 4 中使用逗号作为列表分隔符

.net - 使用 SharpZipLib 解压缩特定文件?

c# - .Net MVC - 从 View 中访问数据库不仅仅是不好的做法?

asp.net-mvc - ASP.NET MVC 路由

c# - 如何编写 lambda 以根据对象中的另一个属性获取一个属性

C# - 简单验证 - DialogResult

c# - 在 C# 中使用 UpdateResource?

C# 立方体/多维数据集