Create an ASP.NET Web API Reporting Service in Bold Reports

Umamageswari Elumalai
Bold Reports
6 min readDec 8, 2022

--

This blog will walk you through how to create a web API service for the Bold Reports Report Viewer component using the ASP.NET Empty Web Application template in a JavaScript application.

The Report Viewer is a visualization control used to display SSRS RDL and RDLC reports within web applications. It allows you to view RDL and RDLC reports with or without using SSRS.

Create an ASP.NET Web API project

  1. First, open Visual Studio 2019, click Create a new project and choose C# from the dropdown.
  2. Select ASP.NET Web Application (.NET Framework) and then click Next.
  3. Change the application name to ReportViewerWebAPI and click Create.
Create a New Project
Create a New Project

4. Select Empty, Web API, and then click OK.

Create a Web API
Create a Web API

5. The web application project is created with the default ASP.NET web template.

Install the NuGet packages

Let’s see how to install the NuGet packages.

  1. Right-click the project in the Solution Explorer tab and choose Manage NuGet Packages.
  2. In the Browse tab, search for the BoldReports.Web package and install it.
Install the BoldReports.Web Package
Install the BoldReports.Web Package

3. The BoldReports.Web package is used to build the server-side implementations.

The following table provides details about the dependency packages and their usage.

Package and purposes
Package and purposes

The dependent packages will be installed automatically on installing the BoldReports.Web package.

Add a web API controller

  1. Right-click the Controller folder in your project and select Add > New Item from the context menu.
Add a Web API Controller
Add a Web API Controller

2. Select Web API Controller Class from the listed templates, name it ReportViewerController.cs, and then click Add.

Select Web API Controller Class
Select Web API Controller Class

3. In the ReportViewerController, add the following using statement.

using BoldReports.Web.ReportViewer;

4. Inherit the IReportController interface and implement the following methods (place 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 initializing the report options before the start of 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
}
}

IReportController interface

We need to declare action methods that are defined in the web API controller for processing RDL, RDLC, and SSRS reports, and for handling resource requests from the Report Viewer control.

The IReportController has the following action method declarations.

Methods and Description
Methods and Description

ReportHelper class

This contains helper methods that help process POST or GET requests from the Report Viewer control and returns the response to the Report Viewer control.

Methods and Description
Methods and Description

Add routing information

To include an action name in the URI, expand the App_Start folder, open the WebApiConfig.cs file, and include the action parameter in the routeTemplate of the Register method.

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 }
);
}
}

The action parameter names the action method on the controller, which is used to map the method in the controller.

Enable cross-origin requests

Browser security prevents Report Viewer from making requests to your web API service when they run in different domains. To allow access to your web API service from a different domain, you must enable cross-origin requests.

  1. Right-click the project in the Solution Explorer tab and choose Manage NuGet Packages.
  2. Search for AspNet.WebApi.Cors NuGet packages and install them in your web API application.
  3. Call EnableCors in WebApiConfig.cs to add cross-origin resource sharing (CORS) services to the Register method. Use the following code to allow any origin requests.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Add EnableCors
config.EnableCors();
// 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 }
);
}
}

4. To specify the CORS policy for the API controller, open the ReportViewerController.cs file and add the following using statement.

using System.Web.Http.Cors;

5. Then, add the [EnableCors] attribute to the controller class.

[EnableCors(origins: "*", headers: "*", methods: "*")]
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 initializing the report options before the start of 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
}
}

6. Compile and build the application.

You can host this service application on your machine. For example, IIS, IIS Express, or Azure can be used for RDL report processing.

Everything required for report processing is ready.

Let us create a simple JavaScript application to test the service action. You can refer to this blog for information on creating a simple JavaScript application.

Add an already created report

Create a Resources folder in the application’s root path. This is where we will keep the RDL report.

In our example, the sales-order-detail.rdl file is added to the Resources folder. You can get the sales-order-detail.rdl file from GitHub here.

Set the ReportPath and service URL

For testing purposes, set the report service URL below, which is relative to the index.html file.

reportServiceUrl: "./api/ReportViewer",
reportPath: "~/Resources/sales-order-detail.rdl"

Register the valid license token

Open the Global.asax.cs file and enter the following code snippet to register the license token in the Application_Start method.

Bold.Licensing.BoldLicenseProvider.RegisterLicense("YOUR LICENSE TOKEN");

You can download license tokens from the Bold Reports site by using our documentation.

Select Embedded Reporting > Generate License Token and copy the token.

In the Global.asax.cs file, register the license token.

To preview the report, build and run the application. You can see that the sales order detail report is loaded in the Bold Report Viewer.

ASP.NET Web API Reporting Service in Report Viewer
ASP.NET Web API Reporting Service in Report Viewer

Conclusion

In this blog, we learned how to create a web API service for the Bold Reports Report Viewer component using the ASP.NET Empty Web Application template in a JavaScript application. To explore this capability further, check out our sample reports and Bold Reports documentation.

If you have any questions, please post them in the comments section below. You can also contact us through our contact page, or if you already have an account, you can log in to submit your support question.

Bold Reports now offers a 15-day free trial with no credit card information required. We welcome you to start a free trial and experience Bold Reports for yourself. Give it a try and let us know what you think!

Stay tuned to our official Twitter, Facebook, LinkedIn, Pinterest, and Instagram pages for announcements about upcoming releases.

Originally published at https://www.boldreports.com on December 8, 2022.

--

--