In this article I will explain with an example, how to use RDLC Reports in ASP.Net Core.
This article makes use of ReportViewerCore.NETCore library for displaying RLDC Report in ASP.Net Core MVC.
Note: For beginners in ASP.Net Core 3, please refer my article ASP.Net Core 3: Hello World Tutorial with Sample Program example.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Designing RDLC Report
RDLC Report Designer does not work with .Net Core projects and hence you need to create an MVC application and then design your RDLC Report.
Note: For details on creating designing Report in ASP.Net MVC, please refer my article Creating RDLC Reports in Visual Studio 2022.
 
RDLC Reports in ASP.Net Core
 
Once report is created it will look as shown below.
RDLC Reports in ASP.Net Core
 
After executing, the Report is displaying data as shown below.
RDLC Reports in ASP.Net Core
 
 
Transferring RDLC Report to ASP.Net Core
To start with, you will need to add a new ASP.Net Core project to the same Solution.
RDLC Reports in ASP.Net Core
 
Now drag and drop (copy and paste) the Report from ASP.Net MVC project to the ASP.Net Core project inside the Reports Folder (Directory) of wwwroot Folder (Directory).
RDLC Reports in ASP.Net Core
 
Once the RDLC Report is dropped, then you can remove the MVC project.
 
 
Installing Nuget Libraries for ReportViewer in .Net Core
First install the Microsoft.CodeAnalysis.Common package using the following command.
Install-Package Microsoft.CodeAnalysis.Common -Version 3.6.0
 
After that, install the ReportViewerCore.NETCore package using the following command.
Install-Package ReportViewerCore.NETCore -Version 15.1.19
 
Note: For more details and documentation about ReportViewerCore.NETCore library please refer the following link.
 
 
Model
The Model class consists of following properties.
public class CustomerModel
{
    public string CustomerID { get; set; }
    public string ContactName { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
}
 
 
Database Context
Once the Entity Framework is configured and connected to the database table, the Database Context will look as shown below.
Note: For beginners in ASP.Net Core 3.1 and Entity Framework, please refer my article ASP.Net Core 3.1: Simple Entity Framework Tutorial with example. It covers all the information needed for connecting and configuring Entity Framework with ASP.Net Core 3.1.
 
using Microsoft.EntityFrameworkCore;
using RDLC_MVC_Core.Models;
 
namespace RDLC_MVC_Core
{
    public class DBCtx : DbContext
    {
        public DBCtx(DbContextOptions<DBCtx> options) : base(options)
        {
        }
 
        public DbSet<CustomerModel> Customers { get; set; }
    }
}
 
 
Namespaces
You need to import the following namespaces.
using System.IO;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Reporting.NETCore;
 
 
Controller
The Controller consists of following Action methods.
Action method for handling GET operation
Inside this Action method, the generic List of CustomerModel class is created and Top 10 records are fetched from the Customers table using Entity Framework and set to it.
Finally, the generic List of CustomerModel class is returned to the View.
 
Action method for handling POST operation
Inside this Action method, first the path of the Report is read using IWebHostingEnvironment interface.
Note: For more details about IWebHostEnvironment interface, please refer Using IWebHostEnvironment in ASP.Net Core.
          For more details on how to access Static Files in .Net Core, please refer my article Static Files (Images, CSS and JS files) in ASP.Net Core.
 
Then, the Report file is read using FileStream class method and Top 10 records are fetched from the Customers table using Entity Framework and stored as IEnumerable collection.
Next, an object of LocalReport class is created and Stream object is passed as parameter to the LoadReportDefinition method.
Now, the object of IEnumerable collection is added in the DataSources property to the LocalRepor class using ReportDataSource object.
Finally, the generated report is converted to BYTE Array using Render method of LocalReport class and returned as File to the browser.
public class HomeController : Controller
{
    private DBCtx Context { get; }
    private IWebHostEnvironment Environment;
    public HomeController(DBCtx _context, IWebHostEnvironment _environment)
    {
        this.Context = _context;
        this.Environment = _environment;
    }
 
    public IActionResult Index()
    {
        List<CustomerModel> customers = (from customer in this.Context.Customers.Take(10)
                                         select customer).ToList();
        return View(customers);
    }
 
    [HttpPost]
    public IActionResult ViewReport()
    {
        string reportFilePath = string.Concat(this.Environment.WebRootPath, "\\Reports\\Report.rdlc");
        using (Stream stream = new FileStream(reportFilePath, FileMode.Open, FileAccess.Read))
        {
            IEnumerable dataSource = (from customer in this.Context.Customers.Take(10)
                                      select customer);
 
            using (LocalReport report = new LocalReport())
            {
                report.LoadReportDefinition(stream);
                report.DataSources.Add(new ReportDataSource("Customers", dataSource));
                byte[] reportData = report.Render("PDF");
                return File(reportData, "application/pdf");
            }
        }
    }
 
 
View
Inside the View, in the very first line the CustomerModel class is declared as IEnumerable which specifies that it will be available as a Collection.
Displaying the Records
For displaying the records, an HTML Table is used.
A FOR EACH loop will be executed over the CustomerModel which will generate the HTML Table rows with the Customer records.
 
Viewing Report
The View consists of an HTML Form with following ASP.Net Tag Helpers attributes.
asp-action – Name of the Action. In this case the name is ViewReport.
asp-controller – Name of the Controller. In this case the name is Home.
method – It specifies the Form Method i.e. GET or POST. In this case it will be set to POST.
Inside the Form there is a Submit button which when clicked the Form is submitted to the ViewReport Action method.
@using RDLC_MVC_Core.Models
@modelIEnumerable<CustomerModel>
@addTagHelper*, Microsoft.AspNetCore.Mvc.TagHelpers
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <table cellpadding="0" cellspacing="0">
        <tr>
            <th>Customer ID</th>
            <th>Contact Name</th>
            <th>City</th>
            <th>Country</th>
        </tr>
        @foreach (CustomerModel customer in Model)
        {
            <tr>
                <td>@customer.CustomerID</td>
                <td>@customer.ContactName</td>
                <td>@customer.City</td>
                <td>@customer.Country</td>
            </tr>
        }
    </table>
    <br />
    <form method="post" asp-controller="Home" asp-action="ViewReport">
        <input type="submit" value="View Report" />
    </form>
</body>
</html>
 
 
Screenshot
RDLC Reports in ASP.Net Core
 
 
Downloads