In this article I will explain with an example, how to use
RDLC Reports in
ASP.Net Core.
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.
Once report is created it will look as shown below.
After executing, the Report is displaying data as shown below.
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.
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).
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 Customer
{
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.
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<Customer> 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.
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<Customer> 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 reportDefinition = 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(reportDefinition);
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
@model IEnumerable<Customer>
@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 (Customer 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
Downloads