In this section, you will learn how to create a Web API Service for Report Viewer using the new ASP.NET Empty Web Application template.
To get start quickly with Web API Service for Report Viewer, you can check on this video:
Right-click the project or solution in the Solution Explorer tab, and choose Manage NuGet Packages. Alternatively, select the Tools > NuGet Package Manager > Manage NuGet Packages for Solution menu command.
Refer to the NuGet Packages section to learn more details about installing and configuring Report Viewer NuGet packages.
Search for BoldReports.Web
NuGet packages, and install them in your Web application.
Package | Purpose |
---|---|
BoldReports.Web |
Builds the server-side implementations. |
The following table provides details about the dependency packages and its usage.
Package | Purpose |
---|---|
Syncfusion.Pdf.AspNet |
Exports the report to a PDF. |
Syncfusion.DocIO.AspNet |
Exports the report to a Word. |
Syncfusion.XlsIO.AspNet |
Exports the report to an Excel. |
Syncfusion.Presentation.AspNet |
Exports the report to an PowerPoint. |
Newtonsoft.Json |
Serializes and deserialize data for the Report Viewer. It is a mandatory package for Report Viewer, and the package version should be higher than 10.0.1 for NET Core 2.0 and others should be higher than 9.0.1. |
The interface IReportController
has declaration of action methods that are defined in the Web API Controller for processing the RDL, RDLC, and SSRS reports and for handling request from the Report Viewer control. The IReportController has the following action methods declaration:
Methods | Description |
---|---|
PostReportAction | Action (HttpPost) method for posting the request in report process. |
OnInitReportOptions | Report initialization method that occurs when the report is about to be processed. |
OnReportLoaded | Report loaded method that occurs when the report and sub report start loading. |
GetResource | Action (HttpGet) method to get resource 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 Controller folder in your project and select Add > New Item from the context menu.
Select Web API Controller Class
from the listed templates and name it as ReportViewerController.cs
Click Add.
While adding Web API Controller class, name 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 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 }
);
}
}
Compile and run the Web API service application.
Browser security prevents Report Viewer from making requests to your Web API Service when both runs in a different domain. To allow access to your Web API service from a different domain, you must enable cross-origin requests.
Right-click the project or solution in the Solution Explorer tab, and choose Manage NuGet Packages. Alternatively, go to Tools > NuGet Package Manager > Manage NuGet Packages for Solution menu command.
Search for Microsoft.AspNet.WebApi.Cors
NuGet packages, and install them in your Web API application.
Call EnableCors
in WebApiConfig
to add CORS services to the Register method. Replace the following code to allow any origin requests.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Add Enable Cors
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 }
);
}
}
To specify the CORS policy for API controller, add the [EnableCors]
attribute to the controller class. Specify the policy name.
[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 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
}
}