Report Designer Server-Side Configuration (ASP.NET Core)

  • 7 minutes to read

This document describes how to create and configure an ASP.NET Core application as a server-side solution to use the End-User Web Report Designer in JavaScript. You can use the DevExpress or Visual Studio template to create an application.

Step 1: Create an ASP.NET Core Application

Use DevExpress Template

You can use the Template Gallery to create a new ASP.NET Core project as follows:

DevExpress Template Step-by-Step
  1. Invoke the DevExpress Template Gallery, select Reporting Application under the ASP .NET Core category and click Create Project.

  2. In the Reporting Control Setting tab, enable the Add Designer Page and Add Report Storage check boxes and click Create Project.

  3. (optionally) Open the ReportStorageWebExtension file and customize the report storage implementation.

    Note

    The template generates a sample storage (a ReportStorageWebExtension descendant) for demonstration purposes only. Create your own implementation for use in production.

    For a detailed tutorial, refer to the following help topic: Create an ASP.NET Core Application with a Report Designer.

Use Microsoft Visual Studio Template

You can create an ASP.NET Core application based on the built-in Visual Studio template and configure it for the DevExpress Report Designer as follows:

Visual Studio Template Step-by-Step
  1. Create a new ASP.NET Core Web Application (or open an existing application).

    Create a new project

  2. Configure a new project.

    Configure a new project

  3. Specify additional information.

    Specify Additional information

  4. Right-click the Dependencies node in the Solution Explorer and select Manage NuGet Packages in the invoked context menu.

    Manage NuGet Packages

  5. Select DevExpress 21.2 Local in the Package source drop-down list and go to the Browse page. Find the DevExpress.AspNetCore.Reporting package v21.2.7 and install it. Select the nuget.org package source and install the Microsoft.AspNetCore.Mvc.NewtonsoftJson package:

    Installed Packages

  6. Open the Startup file and modify it to configure services as demonstrated below.

    using DevExpress.AspNetCore;
    using DevExpress.AspNetCore.Reporting;
    //...
    
    public class Startup {
    //...
       public void ConfigureServices(IServiceCollection services) {
         // Register reporting services in an application's dependency injection container.
          services.AddDevExpressControls();
          // Use the AddMvcCore (or AddMvc) method to add MVC services.
           services.AddMvc(); 
           services.AddNewtonsoftJson();
     }
    
     public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
           // ...
           // Initialize reporting services.
           app.UseDevExpressControls();
            // ...
      }
    }
    
  7. Create the Reports folder and add a report to the application. Use the Save command in the Visual Studio Report Designer smart tag to save the created report to a file in Report Xml (.repx) format in the Reports folder.

  8. Add a server-side report storage. To implement a report storage, add a new class, inherit it from the abstract ReportStorageWebExtension class and override its methods:

    using DevExpress.XtraReports.UI;
    using Microsoft.AspNetCore.Hosting;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    
    namespace ReportDesignerServerSide
    {
        public class CustomReportStorageWebExtension : DevExpress.XtraReports.Web.Extensions.ReportStorageWebExtension
        {
            readonly string ReportDirectory;
            const string FileExtension = ".repx";
            public CustomReportStorageWebExtension(IWebHostEnvironment env)
            {
                ReportDirectory = Path.Combine(env.ContentRootPath, "Reports");
                if (!Directory.Exists(ReportDirectory))
                {
                    Directory.CreateDirectory(ReportDirectory);
                }
            }
    
            public override bool CanSetData(string url) { return true; }
            public override bool IsValidUrl(string url) { return true; }
            public override byte[] GetData(string url)
            {
                try
                {
                    if (Directory.EnumerateFiles(ReportDirectory).
                        Select(Path.GetFileNameWithoutExtension).Contains(url))
                    {
                        return File.ReadAllBytes(Path.Combine(ReportDirectory, url + FileExtension));
                    }
                    throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
                        string.Format("Could not find report '{0}'.", url));
                }
                catch (Exception)
                {
                    throw new DevExpress.XtraReports.Web.ClientControls.FaultException(
                        string.Format("Could not find report '{0}'.", url));
                }
            }
    
            public override Dictionary<string, string> GetUrls()
            {
                return Directory.GetFiles(ReportDirectory, "*" + FileExtension)
                                         .Select(Path.GetFileNameWithoutExtension)
                                         .ToDictionary<string, string>(x => x);
            }
    
            public override void SetData(XtraReport report, string url)
            {
                report.SaveLayoutToXml(Path.Combine(ReportDirectory, url + FileExtension));
            }
    
            public override string SetNewData(XtraReport report, string defaultUrl)
            {
                SetData(report, defaultUrl);
                return defaultUrl;
            }
        }
    }
    

    Review the following help topic for more information and examples: Add Report Storage to ASP.NET Core Application.

  9. Register the report storage implemented in the previous step as a scoped service. Open the Startup.cs file and add the following code in the ConfigureServices method:

    using DevExpress.XtraReports.Web.Extensions;
    // ...
    public void ConfigureServices(IServiceCollection services) {
        //...
        services.AddScoped<ReportStorageWebExtension, CustomReportStorageWebExtension>();
        //...
    }
    

Step 2: Configure the Application

Use CORS Policy

Enable cross-origin requests (CORS) in the newly created ASP.NET Core application. In the ConfigureServices method, specify the policy that allows any local application to access the report’s back-end. Use the SetIsOriginAllowed method to set it up. Open the Startup.cs file and insert the following code:

//...

public class Startup {
//...
    public void ConfigureServices(IServiceCollection services) {
        // ...
            services.AddCors(options => {
                options.AddPolicy("AllowCorsPolicy", builder => {
                    // Allow all ports on local host.
                    builder.SetIsOriginAllowed(origin => new Uri(origin).Host == "localhost");
                    builder.WithHeaders("Content-Type");
                });
            });
        // ...
    }
// ...
}

In the Configure method, call the UseCors method.

Important

The UseCors method should be called after the UseRouting method and before any MVC-related code. Place the UseCors method before the UseMvc or UseEndpoints methods.

Pass the policy name as a parameter:

//...
public class Startup {
//...
    public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
        // ...
        app.UseRouting();
        // ...
        app.UseCors("AllowCorsPolicy");
        // ...
        app.UseEndpoints(endpoints => {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");
        });
    }
// ...
}

Add Controllers

To implement custom controllers, inherit from the base controller class and specify a custom route.

The following table lists base controller classes that process requests from reporting components, default routes, and methods that allow you to specify custom routes:

Component Base Controller Class Predefined Route Custom Route
Report Designer ReportDesignerController DXXRD ReportDesignerHandlerUri(String)
Document Viewer WebDocumentViewerController DXXRDV ReportPreviewHandlerUri(String)
Query Builder QueryBuilderController DXXQB QueryBuilderHandlerUri(String)

An application with End-User Report Designer requires all three controllers for proper operation.

The following code snippet implements controllers for the web report designer:

using DevExpress.AspNetCore.Reporting.QueryBuilder;
using DevExpress.AspNetCore.Reporting.ReportDesigner;
using DevExpress.AspNetCore.Reporting.WebDocumentViewer;
using DevExpress.AspNetCore.Reporting.WebDocumentViewer.Native.Services;
using DevExpress.AspNetCore.Reporting.ReportDesigner.Native.Services;
using DevExpress.AspNetCore.Reporting.QueryBuilder.Native.Services;
using DevExpress.XtraReports.Web.ReportDesigner;
using Microsoft.AspNetCore.Mvc;

namespace DXWebApplication5.Controllers {
    public class CustomWebDocumentViewerController : WebDocumentViewerController {
        public CustomWebDocumentViewerController(IWebDocumentViewerMvcControllerService controllerService) : base(controllerService) {
        }
    }

    public class CustomReportDesignerController : ReportDesignerController {
        public CustomReportDesignerController(IReportDesignerMvcControllerService controllerService) : base(controllerService) {
        }
        [HttpPost("[action]")]
        public IActionResult GetDesignerModel([FromForm]string reportUrl, [FromServices] IReportDesignerClientSideModelGenerator modelGenerator) {
            var model = modelGenerator.GetModel(reportUrl, null, ReportDesignerController.DefaultUri, WebDocumentViewerController.DefaultUri, QueryBuilderController.DefaultUri);
            return DesignerModel(model);
        }
    }

    public class CustomQueryBuilderController : QueryBuilderController {
        public CustomQueryBuilderController(IQueryBuilderMvcControllerService controllerService) : base(controllerService) {
        }
    }
}

This implementation overrides the GetDesignerModel method in the CustomReportDesignerController class. However, the default ReportDesignerController has a DXXRD predefined path. This means that on the client you should set the getDesignerModelAction property to the DXXRD/GetDesignerModel path.

The following code snippet specifies the getDesignerModelAction property in an Angular application:

export class AppComponent {
    title = 'DXReportDesignerSample';
    getDesignerModelAction = "DXXRD/GetDesignerModel";
    reportUrl = "TestReport?testId";
    hostUrl = 'http://localhost:1858/';
}

For more code samples with custom controllers, review the following example:

View Example: ASP.NET Core Reporting - Best Practices

Add Data Sources

To add data sources so that they become available for the end-user in th Report Designer, modify the GetDesignerModel method as follows:

using System.Collections.Generic;
using DevExpress.DataAccess.Sql;

public class ReportDesignerController : ReportDesignerApiController {
    public class CustomReportDesignerController : ReportDesignerController {
        public CustomReportDesignerController(IReportDesignerMvcControllerService controllerService) : base(controllerService) {
        }

        [HttpPost("[action]")]
        public IActionResult GetDesignerModel([FromForm]string reportUrl, [FromServices] IReportDesignerClientSideModelGenerator modelGenerator) {
            var dataSources = new Dictionary<string, object>();
            var ds = new SqlDataSource("NWindConnectionString");

            // Create a SQL query to access the Products data table.
            SelectQuery query = SelectQueryFluentBuilder.AddTable("Products").SelectAllColumnsFromTable().Build("Products");
            ds.Queries.Add(query);
            ds.RebuildResultSchema();
            dataSources.Add("Northwind", ds);

            var model = modelGenerator.GetModel(reportUrl, dataSources, ReportDesignerController.DefaultUri, WebDocumentViewerController.DefaultUri, QueryBuilderController.DefaultUri);
            return DesignerModel(model);
        }
    }
  • If you started with the DevExpress template to create an application, the project already contains the SQLite database and the NWindConnectionString is a connection string for this database.

  • If you started with the Microsoft generic template, create the NorthWind sample database on the local SQL Server and add the following element to the appsettings.json file:

    "ConnectionStrings": {
    "NWindConnectionString": "XpoProvider=MSSqlServer;data source=localhost;integrated security=SSPI;initial catalog=Northwind"
    }
    

Run the Application

Step 3: Determine the Host URL

Examine the address string in the browser to determine the host URL and application port.

You can use the Debug section of the Project Properties dialog to view and change the port number:

If you do not use Visual Studio to run the project, inspect the launchSettings.json file. If your application uses HTTPS redirection and calls the UseHttpsRedirection method at startup, the port number is specified in the sslPort setting.

The URL and port number are required for the host setting when you configure the client application as described in the following help topics:

See Also