This section explains you the steps required to create your first ASP.NET MVC reporting application to display an already created SSRS RDL report in the Bold Reports® ASP.NET MVC Report Viewer Classic without using a Report Server.
To get start quickly with Report Viewer Classic, you can check on this video:
Open Visual Studio 2017, click the File menu, go to New, and then select Project.
Go to Installed > Visual C# > Web, and then select the required .NET Framework in the drop-down.
Select ASP.NET Web Application (.NET Framework), change the application name, and then click OK.
Choose MVC and Web API, and then click OK.
Right-click the project or solution on the Solution Explorer tab, and choose Manage NuGet Packages. Alternatively, select Tools > NuGet Package Manager > Manage NuGet Packages for Solution.
Search for BoldReports.Web
, BoldReports.JavaScript
, and BoldReports.Mvc5
NuGet packages, and install them in your MVC application.
Package | Purpose |
---|---|
BoldReports.Web |
Creates Web API service to process the reports. |
BoldReports.Mvc5 |
Contains HTML helpers to create client-side Report Viewer control. |
BoldReports.JavaScript |
Contains reporting components scripts and style sheets. |
Open the ~/Views/Web.config
file and add the BoldReports.Mvc
assembly reference to the <system.web.webPages.razor>
element.
<configuration>
....
....
<system.web.webPages.razor>
....
....
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
....
....
<add namespace="BoldReports.Mvc"/>
</namespaces>
</pages>
</system.web.webPages.razor>
....
....
</configuration>
Set the value of UnobtrusiveJavaScriptEnabled
to false in the Root directory web.config
file as demonstrated in the following code example.
<configuration>
<appSettings>
......
......
<add key="UnobtrusiveJavaScriptEnabled" value="false" />
</appSettings>
......
......
</configuration>
If you want to use ‘UnobtrusiveJavaScriptEnabled’, then use the ej.unobtrusive.min.js script with your application. You can get the script from the installed location as shown in the following image.
Install the BoldReports.JavaScript
and BoldReports.Mvc5
nuget packages into your application.
BoldReports.JavaScript
- contains Report Viewer Classic scripts and style sheets.BoldReports.Mvc5
- contains HTML Helpers for Report Viewer Classic.Successful installation adds the scripts and styles to the Scripts
and Content
folders in your application.
The following scripts and style sheets are mandatorily required to use the Report Viewer Classic.
bold.reports.all.min.css
jquery.min.js
ej.chart.min.js
- Renders the chart item. Add this script, only if your report contains the chart report item.ej2-base.min.js
, ej2-data.min.js
, ej2-pdf-export.min.js
, ej2-svg-base.min.js
, ej2-lineargauge.min.js
, and ej2-circulargauge.min.js
- Render the gauge item. Add these scripts only if your report contains the gauge report item.ej2-maps.min.js
- Renders the map item. Add this script only if your report contains the map report item.bold.reports.common.min.js
bold.reports.widgets.min.js
ej.chart.min.js
- Renders the chart item. Add this script only if your report contains the chart report item.bold.report-viewer.min.js
Open the \Views\Shared\_Layout.cshtml
page.
Add the listed references in the same order given in the above list. You can replace the following code in the \Views\Shared\_Layout.cshtml
page.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
@Styles.Render("~/Content/css")
@Styles.Render("~/Content/bold-reports/material/bold.reports.all.min.css")
@Scripts.Render("~/bundles/modernizr")
</head>
<body>
<div style="min-height: 600px;width: 100%;">
@RenderBody()
</div>
@Scripts.Render("~/bundles/jquery")
@Scripts.Render("~/bundles/bootstrap")
<!--Render the gauge item. Add these scripts only if your report contains the gauge report item.-->
@Scripts.Render("~/Scripts/bold-reports/common/ej2-base.min.js")
@Scripts.Render("~/Scripts/bold-reports/common/ej2-data.min.js")
@Scripts.Render("~/Scripts/bold-reports/common/ej2-pdf-export.min.js")
@Scripts.Render("~/Scripts/bold-reports/common/ej2-svg-base.min.js")
@Scripts.Render("~/Scripts/bold-reports/data-visualization/ej2-circulargauge.min.js")
@Scripts.Render("~/Scripts/bold-reports/data-visualization/ej2-lineargauge.min.js")
<!--Renders the map item. Add this script only if your report contains the map report item.-->
@Scripts.Render("~/Scripts/bold-reports/data-visualization/ej2-maps.min.js")
@Scripts.Render("~/Scripts/bold-reports/common/bold.reports.common.min.js")
@Scripts.Render("~/Scripts/bold-reports/common/bold.reports.widgets.min.js")
<!--Renders the chart item. Add this script only if your report contains the chart report item.-->
@Scripts.Render("~/Scripts/bold-reports/data-visualization/ej.chart.min.js")
<!-- Report Viewer component script-->
@Scripts.Render("~/Scripts/bold-reports/bold.report-viewer.min.js")
@RenderSection("scripts", required: false)
@Html.Bold().ScriptManager()
</body>
</html>
Open the ~/Views/Shared/_Layout.cshtml
page and add the Script Manager at the end of the <body>
element as in the following code sample.
<body>
....
....
<!-- Bold Reports<sup>®</sup> script manager -->
@Html.Bold().ScriptManager()
</body>
Using the Bold()
tag, add the Bold Report Viewer Classic component to any web page (cshtml
) of your application in the ~/Views
folder. For example, the Index.cshtml
page can be replaced with the following code by removing the existing codes.
@(Html.Bold().ReportViewer("viewer"))
Create a folder Resources
in your application to store the RDL reports.
Add already created reports to the newly created folder.
In this tutorial, the
sales-order-detail.rdl
report is used, and it can be downloaded at this link.
The interface IReportController
has a declaration of action methods that are defined in the Web API Controller for processing the RDL, RDLC, and SSRS reports and for handling requests from the Report Viewer control. The IReportController has the following action methods declaration.
Methods | Description |
---|---|
PostReportAction | Action (HttpPost) method for posting the request in the report process. |
OnInitReportOptions | Report initialization method occurs when the report is about to be processed. |
OnReportLoaded | Report loaded method occurs when the report and sub report start loading. |
GetResource | Action (HttpGet) method is used to get resources for the report. |
The class ReportHelper
contains helper methods that help to process a Post or Get request from the Report Viewer control and return the response to the Report Viewer control. It has the following methods:
Methods | Description |
---|---|
GetResource | Returns the report resource to the requested key. |
ProcessReport | Processes the report request and returns the result. |
Right-click the Controller folder in your project and select Add > New Item from the context menu.
Select the Web API Controller Class
from the listed templates and name it as ReportViewerController.cs
.
Click Add.
While adding the Web API Controller class, naming it with the suffix
Controller
that is mandatory.
Open the ReportViewerController
and add the following using statement.
using BoldReports.Web.ReportViewer;
Inherit the IReportController
interface, and implement its methods (replace the following code in the newly created Web API controller).
public class ReportViewerController : ApiController, IReportController
{
// Post action for processing the RDL/RDLC report
public object PostReportAction(Dictionary<string, object> jsonResult)
{
return ReportHelper.ProcessReport(jsonResult, this);
}
// Get action for getting resources from the report
[System.Web.Http.ActionName("GetResource")]
[AcceptVerbs("GET")]
public object GetResource(string key, string resourcetype, bool isPrint)
{
return ReportHelper.GetResource(key, resourcetype, isPrint);
}
// Method that will be called when initialize the report options before start processing the report
[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
// You can update report options here
}
// Method that will be called when reported is loaded
[NonAction]
public void OnReportLoaded(ReportViewerOptions reportOption)
{
//You can update report options here
}
}
To configure routing to include an action name in the URI, open the WebApiConfig.cs
file and change the routeTemplate
in the Register method as follows.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
To render the reports available in the application, set the ReportPath
and ReportServiceUrl
properties of the Report Viewer. You can replace the following code on your Report Viewer page.
@(Html.Bold().ReportViewer("viewer")
.ReportServiceUrl("/api/ReportViewer")
.ReportPath("~/Resources/sales-order-detail.rdl")
)
The report path property is set for the RDL report that is added to the project
Resources
folder.