Search results

Create ASP.NET Core Web API Service

In this section, you will learn how to create a ASP.NET Core Web API for Report Designer using the new ASP.NET Core Web Application template.

  1. Open Visual Studio 2017, click the File menu, go to New, and then select Project.
  2. Go to Installed > Visual C# > Web, and then select ASP.NET Core Web Application, change the application name, and then click OK. Creating a new ASP.NET Core Web Application Project

    The ReportDesigner Service support from core 2.0 framework. So, should choose the framework version 2.0 or higher versions for Designer API creation.

  3. Choose the ASP.NET Core version and select the API application template and click OK. Do not select Enable Docker Support. Creating a new ASP.NET Core Application Project

List of dependency Libraries

The Web API service configuration requires the following reporting server-side packages. In the Solution Explore, right-click the Dependencies, select Manage NuGet Packages and then search the package Syncfusion.Reporting.Net.Core and install to the application. The following provides detail of the packages and its usage.

Package Purpose
Syncfusion.Reporting.Net.Core Builds the server-side implementations.

If we install the above NuGet packages, it automatically add the below ReportDesigner dependent NuGet file in your application.

Package Purpose
Syncfusion.Compression.Net.Core Supports for exporting the report to PDF, Microsoft Word, and Microsoft Excel format. It is a base library for the packages Syncfusion.Pdf.Net.Core , Syncfusion.DocIO.Net.Core and Syncfusion.XlsIO.Net.Core.
Syncfusion.Pdf.Net.Core Supports for exporting the report to a PDF.
Syncfusion.DocIO.Net.Core Supports for exporting the report to a Word.
Syncfusion.XlsIO.Net.Core Supports for exporting the report to an Excel.
Syncfusion.OfficeChart.Net.Core It is a base library of the Syncfusion.XlsIO.Net.Core package.
Newtonsoft.Json Serialize and deserialize the data or report for report designer. It is a mandatory package for the report designer, and the package version should be higher of 10.0.1 for NET Core 2.0 and others should be higher of 9.0.1.
System.Data.SqlClient This is an optional package for the report viewer and designer. It should be referred in project when process the RDL report and which contains the SQL Server and SQL Azure data source. Also, the package version should be higher of 4.1.0 .

Refer to the NuGet Packages section to learn more details about installing and configuring Report Designer NuGet packages.

Register CORS

Browser security prevents Report Designer from making requests to your API Service when both runs in a different domain. To allow access to your API service from a different domain, you must enable cross-origin requests.

Register AddCors in Startup.ConfigureServices to add CORS services to the app’s service container. Replace the following code to allow any origin requests.

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddCors(o => o.AddPolicy("AllowAllOrigins", builder =>
        {
            builder.AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader();
        }));
        services.AddMvc();
    }

For more information about CORS, see Configure CORS for API

Add API Service

  1. Right-click the project and select Add > New Item from the context menu.
  2. In the Add New Item dialog, select API Controller class and name it as ReportingAPIController, and then click Add. Adding a new controller to the project

While adding API Controller class, name it with the suffix Controller that is mandatory.

Configure Web API

The IReportDesignerController interface contains the required actions and helper methods declaration to process the designer file and data actions. The ReportDesignerHelper and ReportHelper class contains methods that help to process Post or Get request from the control and return the response.

Methods Description
PostDesignerAction Action (HttpPost) method for posting the request for designer actions.
UploadReportAction Action (HttpPost) method for posted file actions.
UploadFile File upload method that is call while upload a file.
GetImage Action (HttpGet) method for getting resource for report images.
GetFilePath Get the path of specific file
GetFile Get the Specific file information
GetFiles Get the file list based on file type
PostReportAction Action (HttpPost) method for posting the request for report process.
GetResource Action (HttpGet) method for getting resource for report.
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.

ReportDesignerHelper

The class ReportDesignerHelper contains helper methods that help to process Post or Get request from the Report Designer control and returns the response to the Report Designer control. It has the following methods.

Methods Description
GetResource Returns the report resource for the requested key.
ProcessReport Processes the report request and returns the result.

ReportHelper

The class ReportHelper contains helper methods that help process Post or Get request for report preview action and returns the response to the Report Designer. It has the following methods.

Methods Description
GetResource Returns the report resource for the requested key.
ProcessReport Processes the report request and returns the result.
  1. Open the ReportingAPIController and add the following using statement.

    using Syncfusion.Reporting.Web.ReportViewer;
    using Syncfusion.Reporting.Web.ReportDesigner;
  2. Add the {action} placeholder in the route template, if not contains with default controller attribute.

    [Route("api/[controller]/[action]")]
    public class ReportingAPIController : Controller
    {
    
    }
  3. Next, add the [EnableCors] attribute to the ReportingAPIController class and specify the policy name which given in Startup.ConfigureServices.

    [Microsoft.AspNetCore.Cors.EnableCors("AllowAllOrigins")]
    [Route("api/[controller]/[action]")]
    public class ReportingAPIController : Controller, Syncfusion.Reporting.Web.ReportDesigner.IReportDesignerController
    {
    }
  4. Inherit the IReportDesignerController interface, and implement its methods (replace the following code in newly created Web API controller).

    [Microsoft.AspNetCore.Cors.EnableCors("AllowAllOrigins")]
    [Route("api/[controller]/[action]")]
    public class ReportingAPIController : Controller, Syncfusion.Reporting.Web.ReportDesigner.IReportDesignerController
    {
        private Microsoft.Extensions.Caching.Memory.IMemoryCache _cache;
        private Microsoft.AspNetCore.Hosting.IHostingEnvironment _hostingEnvironment;
    
        public ReportingAPIController(Microsoft.Extensions.Caching.Memory.IMemoryCache memoryCache, Microsoft.AspNetCore.Hosting.IHostingEnvironment hostingEnvironment)
        {
            _cache = memoryCache;
            _hostingEnvironment = hostingEnvironment;
        }
    
        public string GetFilePath(string fileName)
        {
            string targetFolder = this._hostingEnvironment.WebRootPath + "\\";
            targetFolder += "Cache";
    
            if (!System.IO.Directory.Exists(targetFolder))
            {
                System.IO.Directory.CreateDirectory(targetFolder);
            }
    
            if (!System.IO.Directory.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken))
            {
                System.IO.Directory.CreateDirectory(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken);
            }
    
            var folderPath = targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\";
            return folderPath + fileName;
        }
    
        public object GetImage(string key, string image)
        {
            return ReportDesignerHelper.GetImage(key, image, this);
        }
    
        public object GetResource(ReportResource resource)
        {
            return ReportHelper.GetResource(resource, this, _cache);
        }
    
        public void OnInitReportOptions(ReportViewerOptions reportOption)
        {
    
        }
    
        public void OnReportLoaded(ReportViewerOptions reportOption)
        {
    
        }
    
        [HttpPost]
        public object PostDesignerAction([FromBody] Dictionary<string, object> jsonResult)
        {
            return ReportDesignerHelper.ProcessDesigner(jsonResult, this, null, this._cache);
        }
    
        public object PostFormDesignerAction()
        {
            return ReportDesignerHelper.ProcessDesigner(null, this, null, this._cache);
        }
    
        public object PostFormReportAction()
        {
            return ReportHelper.ProcessReport(null, this, this._cache);
        }
    
        [HttpPost]
        public object PostReportAction([FromBody] Dictionary<string, object> jsonResult)
        {
            return ReportHelper.ProcessReport(jsonResult, this, this._cache);
        }
    
        public bool UploadFile(IFormFile httpPostedFile)
        {
            string targetFolder = this._hostingEnvironment.WebRootPath + "\\";
            string fileName = !string.IsNullOrEmpty(ReportDesignerHelper.SaveFileName) ? ReportDesignerHelper.SaveFileName : System.IO.Path.GetFileName(httpPostedFile.FileName);
            targetFolder += "Cache";
    
            if (!System.IO.Directory.Exists(targetFolder))
            {
                System.IO.Directory.CreateDirectory(targetFolder);
            }
    
            if (!System.IO.Directory.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken))
            {
                System.IO.Directory.CreateDirectory(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken);
            }
            using (var reader = new System.IO.StreamReader(httpPostedFile.OpenReadStream()))
            {
                string contentAsString = reader.ReadToEnd();
                byte[] bytes = System.Text.Encoding.ASCII.GetBytes(contentAsString);
                if (System.IO.File.Exists(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\" + fileName))
                {
                    System.IO.File.Delete(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\" + fileName);
                }
                System.IO.File.WriteAllBytes(targetFolder + "\\" + ReportDesignerHelper.EJReportDesignerToken + "\\" + fileName, bytes);
                reader.Close();
                reader.Dispose();
            }
            return true;
        }
    
        [HttpPost]
        public void UploadReportAction()
        {
            ReportDesignerHelper.ProcessDesigner(null, this, this.Request.Form.Files[0], this._cache);
        }
    
        public FileModel GetFile(string filename, bool isOverride)
        {
            throw new NotImplementedException();
        }
    
        public List<FileModel> GetFiles(FileType fileType)
        {
            throw new NotImplementedException();
        }
    }
  5. Compile and run the Web API service application.