In this article I will explain with an example, how to display list of files from Folder (Directory) in ASP.Net Core (.Net Core) Razor Pages.
Note: For beginners in ASP.Net Core (.Net Core 7) Razor Pages, please refer my article ASP.Net Core 7 Razor Pages: Hello World Tutorial with Sample Program example.
 
 

Files Folder (Directory) Location

The files are located inside the Files Folder (Directory) of wwwroot Folder (Directory).
ASP.Net Core Razor Pages: Display List of Files from Folder (Directory)
 
 

Model

The Model class consist of following property.
public class FileModel
{
    public string FileName { getset; }
}
 
 

Razor PageModel (Code-Behind)

The IndexModel consists of following Handler methods.

Handler method for handling GET operation

Inside this Handler method, the path of all files in the Files Folder (Directory) which is present inside the wwwroot Folder (explained earlier) is fetched using the IWebHostEnvironment interface into a String Array using the GetFiles method of the Directory class.
Note: For more details on how IWebHostEnvironment interface, please refer my article Using IWebHostEnvironment in ASP.Net Core.
Then, each path is added as new object to the Generic List collection of FileModel class object.
Note: For more details on how to access Static Files in ASP.Net Core (.Net Core), please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
 

Handler method for handling POST operation

This Handler method gets called when Anchor link is clicked.
Note: The following Handler method performs File Download and hence the return type is set to FileResult.
 
This Handler method accepts, the name of the file which will be downloaded as parameter.
Then, the BYTE Array is determined using ReadAllBytes method of the File class.
Finally, the BYTE array along with the content type and file name is passed as parameter to File function which downloads the file.
public class IndexModel : PageModel
{
    public List<FileModel> Files{ getset; }
 
    private IWebHostEnvironment Environment;
    public IndexModel(IWebHostEnvironment _environment)
    {
        this.Environment = _environment;
    }
 
    public void OnGet()
    {
        //Fetch all files in the Folder (Directory).
        string[] filePaths Directory.GetFiles(Path.Combine(this.Environment.WebRootPath, "Files/"));
 
        //Copy File names to Model collection.
        this.Files = new List<FileModel>();
        foreach (string filePath in filePaths)
        {
            this.Files.Add(new FileModel { FileName = Path.GetFileName(filePath) });
        }
    }
 
    public FileResult OnGetDownloadFile(string fileName)
    {
        //Build the File Path.
        string path = Path.Combine(this.Environment.WebRootPath, "Files/") + fileName;
 
        //Read the File data into Byte Array.
        byte[] bytes = System.IO.File.ReadAllBytes(path);
 
        //Send the File to Download.
        return File(bytes, "application/octet-stream", fileName);
    }
}
 
 

Razor Page (HTML)

HTML Markup

Inside the Razor Page, the FileModel class is declared as Generic List collection which specifies that it will be available as a Collection.

Displaying the Files

For displaying the files, an HTML Table is used. A FOR EACH loop will be executed over the Model which will generate the HTML Table rows with the File records.
 

Downloading the File

The HTML Table contains an HTML Anchor Link for downloading the File.
The href property of the Anchor Link is set using the @Url.Page method which accepts the following parameters:
1. Name of the PageModel class.
2. Name of the Handler method.
3. The name and the value of the Parameters to be passed to the Handler method.
When the Download link is clicked, the DownloadFile Handler method is called which initiates the File is download operation.
Note: In the Razor PageModel, the Handler method name is OnGetDownloadFile but here it will be specified as DownloadFile when calling from the Razor HTML Page.
 
@page
@addTagHelper*,Microsoft.AspNetCore.Mvc.TagHelpers
@model Razor_File_Download.Pages.IndexModel
@using Razor_File_Download.Models
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <th>File Name</th>
            <th></th>
        </tr>
        @foreach (FileModel file in Model.Files)
        {
            <tr>
                <td>@file.FileName</td>
                <td><a href="@Url.Page("Index""DownloadFile"new { fileName file.FileName })">Download</a></td
            </tr>
        }
    </table>
</body>
</html>
 
 

Screenshot

ASP.Net Core Razor Pages: Display List of Files from Folder (Directory)
 
 

Downloads