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

  • 6 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 DevExpress ASP.NET MVC Project Wizard, enable the Create Designer Page and Create 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).
  1. Right-click the Dependencies node in the Solution Explorer and select Manage NuGet Packages in the invoked context menu.

  2. Select DevExpress 20.2 Local in the Package source drop-down list and go to the Browse page. Find the DevExpress.AspNetCore.Reporting package and install it.

  3. 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.AddMvcCore(); 
     }
    
     public void Configure(IApplicationBuilder app, IHostingEnvironment env) {
           // ...
           app.UseStaticFiles();
           // Initialize reporting services.
           app.UseDevExpressControls();
            // ...
      }
    }
    
  4. Add reports to the application.

  5. Provide 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. See the following help topic for more information and examples: Add Report Storage to ASP.NET Core Application.

  6. Add 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

Add 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 a Controller

Create a controller and add an action to create the Report Designer model. In this action, use the ReportDesignerClientSideModelGenerator class and provide the following initialization data:

  • the report name (reportURL) (required);
  • available data sources (optional);
  • default reporting controller URIs (required).
using Microsoft.AspNetCore.Mvc;
using DevExpress.XtraReports.Web.ReportDesigner;
//...   
public class ReportDesignerController : Controller {
    //...
    public ActionResult GetReportDesignerModel([FromForm] string reportUrl,
    [FromServices] IReportDesignerClientSideModelGenerator reportDesignerClientSideModelGenerator)
    {
        string modelJsonScript =
            reportDesignerClientSideModelGenerator
            .GetJsonModelScript(
                // The name of a report (reportUrl)
                // that the Report Designer opens when the application starts.
                reportUrl,
                // Data sources for the Report Designer.                
                GetAvailableDataSources(),
                // The URI path of the default controller
                // that processes requests from the Report Designer.
                DevExpress.AspNetCore.Reporting.ReportDesigner.ReportDesignerController.DefaultUri,
                // The URI path of the default controller
                // that processes requests from the Web Document Viewer.
                DevExpress.AspNetCore.Reporting.WebDocumentViewer.WebDocumentViewerController.DefaultUri,
                // The URI path of the default controller
                // that processes requests from the Query Builder.
                DevExpress.AspNetCore.Reporting.QueryBuilder.QueryBuilderController.DefaultUri
            );
        return Content(modelJsonScript, "application/json");
    }
}

You can customize controllers and change routes, but it is not necessary in most situations. The following table lists controllers that process requests from reporting components, and their default routes:

Component Controller Predefined Route
Report Designer ReportDesignerController DXXRD
Document Viewer WebDocumentViewerController DXXRDV
Query Builder QueryBuilderController DXXQB

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

To implement a custom controller, create a descendant of the class listed above and use the HandlerUri property to specify the route:

Component Handler Uri
Report Designer ReportDesignerHandlerUri, ReportPreviewHandlerUri, QueryBuilderHandlerUri
Document Viewer HandlerUri

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

View Example: ASP.NET Core Reporting - Best Practices

Add Data Sources

Implement a method declared in the previous step that creates data sources for the Report Designer (the GetAvailableDataSources method in this example).

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

public class ReportDesignerController : ReportDesignerApiController {
    // ...

    Dictionary<string, object> GetAvailableDataSources() {
        var dataSources = new Dictionary<string, object>();
        SqlDataSource ds = new SqlDataSource("NWindConnectionString");
        var query = SelectQueryFluentBuilder.AddTable("Products").SelectAllColumns().Build("Products");
        ds.Queries.Add(query);
        ds.RebuildResultSchema();
        dataSources.Add("SqlDataSource", ds);
        return dataSources;
    }
}

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