Cannot download files in .Net 6

Dmitriy Reznik 236 Reputation points
2022-06-02T13:07:25.767+00:00

I have an MVC .Net Core 2.1 application. It has an action that generates a .csv file and returns it to download. Now, as this .Net version is not supported, I created a new application with .NET 6, and moved there my controllers, views etc. from the old application. Now for some reason download does not work. The execution does not even reach the action, and I get this error:

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

So it is looking for the view by the action name, but this view obviously does not exist.

Here is my code:

_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:

    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();

This is how it looked in .Net 2.1 (before migrating to .NET 6):

Startup.cs

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

How can I prevent attempts to load the view that does not exist, and just let the file to be downloaded?

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,189 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 26,136 Reputation points
    2022-06-02T13:35:14.797+00:00

    Remove "Async" from the end of the URL.

                 <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>
    

0 additional answers

Sort by: Most helpful