I have my API project with ASP.NET Core
(.NET5). Everything is working fine and the Swagger page shows all the APIs. Now, in a controller I want to add a function to export an Excel file. The code in the controller is this:
[HttpGet]
public IActionResult DownloadExcel()
{
byte[] reportBytes;
using (var package = _utils.CreateCommentPackage())
{
reportBytes = package.GetAsByteArray();
}
return File(reportBytes, XlsxContentType,
$"Comments-{DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}.xlsx");
}
Pretty simple but the Swagger definition is not created. When I open the Swagger page I see that
If I remove this function, Swagger is working again.
You need add a route, change your code like following;
[HttpGet]
[Route("api/DownloadExcel")]
public IActionResult DownloadExcel()
{
byte[] reportBytes;
using (var package = _utils.CreateCommentPackage())
{
reportBytes = package.GetAsByteArray();
}
return File(reportBytes, XlsxContentType,
$"Comments-{DateTime.Now.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture)}.xlsx");
}