无法在 .Net 6 中下载文件

Cannot download files in .Net 6

我有一个 MVC .Net Core 2.1 应用程序。它有一个生成 .csv 文件并 returns 下载的操作。现在,由于不支持此 .Net 版本,我使用 .NET 6 创建了一个新应用程序,并将我的控制器、视图等从旧应用程序移到那里。现在由于某种原因下载不起作用。执行甚至没有达到动作,我得到这个错误:

No webpage was found for the web address: https://localhost:44398/Reports/InventoryReportAsync HTTP ERROR 404

原来是通过action名来找视图,但是这个视图显然不存在

这是我的代码:

_Layout.cshtml

      <div class="dropdown">
        <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Reports</button>
        <ul class="dropdown-menu">
            <li><a class="dropdown-item" asp-controller="Reports" asp-action="InventoryReportAsync">Inventory Report</a></li>
            <li><a class="dropdown-item" asp-controller="Reports" asp-action="ItemReorderReportAsync">Item Reorder Report</a></li>
            <li><a class="dropdown-item" asp-controller="Reports" asp-action="OrderHistoryReport">Order History Report</a></li>
        </ul>
    </div>

ReportsController.cs

    public async Task<IActionResult> InventoryReportAsync()
    {
        try
        {
            List<InventoryReportDto> reportModel = await _itemRepository.GetInventoryReportDtosAsync();

            return await Export<InventoryReportDto, InventoryReportCsvMap>(reportModel, "InventoryReport.csv");
        }
        catch (Exception ex)
        {
            return StatusCode(500, new { ex.Message });
        }
    }

    protected async Task<IActionResult> Export<T, M>(List<T> exportModel, string fileName) where T : class where M : ClassMap
    {
        MemoryStream stream = new MemoryStream();
        StreamWriter streamWriter = new StreamWriter(stream)
        {
            AutoFlush = true
        };
        CsvWriter csv = new CsvWriter(streamWriter, CultureInfo.InvariantCulture);

        csv.Context.RegisterClassMap<M>();

        csv.WriteRecords(exportModel);

        await csv.FlushAsync();

        stream.Position = 0;

        return File(stream, "application/octet-stream", fileName);
    }

已添加:

Program.cs

app.MapControllerRoute(
   name: "default",
   pattern: "{controller=Home}/{action=Index}/{id?}");
app.MapRazorPages();

这是它在 .Net 2.1 中的样子:

Startup.cs

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

我把这个问题发到别处,在这里得到了答案:

https://docs.microsoft.com/en-us/answers/questions/874470/cannot-download-files-in-net-6.html

我只需要从此处的操作名称中删除“Async”后缀:

 <div class="dropdown">
    <button type="button" class="btn btn-primary dropdown-toggle" data-bs-toggle="dropdown" aria-expanded="false">Reports</button>
    <ul class="dropdown-menu">
        <li><a class="dropdown-item" asp-controller="Reports" asp-action="InventoryReport">Inventory Report</a></li>
        <li><a class="dropdown-item" asp-controller="Reports" asp-action="ItemReorderReport">Item Reorder Report</a></li>
        <li><a class="dropdown-item" asp-controller="Reports" asp-action="OrderHistoryReport">Order History Report</a></li>
    </ul>
</div>