This project demonstrates how you can incorporate a DevExpress Dashboard component into a client-side app built with React. Use it as a template when you need to create a similar web application.
The example uses a modular client-server approach. The server (backend) project communicates with the client (frontend) application that includes all the necessary styles, scripts and HTML templates. Note that the script version on the client must match the version of libraries on the server.
- The asp-net-core-server folder contains the backend project that targets .NET 8.
- The dashboard-react-app folder contains the client application built with React.
Quick Start
Server
In the asp-net-core-server folder run the following command:
Codedotnet run
The server starts at http://localhost:5000
and the client gets data from http://localhost:5000/api/dashboard
. To debug the server, run the asp-net-core-server application in Visual Studio and change the client's endpoint
property according to the listening port: https://localhost:44307/api/dashboard
.
See the following section for information on how to install NuGet packages from the DevExpress NuGet feed: Install DevExpress Controls Using NuGet Packages.
This server allows CORS requests from all origins with any scheme (http or https). This default configuration is insecure: any website can make cross-origin requests to the app. We recommend that you specify the client application's URL to prohibit other clients from accessing sensitive information stored on the server. Learn more: Cross-Origin Resource Sharing (CORS)
Client
In the dashboard-react-app folder, run the following commands:
Codenpm install
npm run dev
Open the address shown in console in your browser to see the result.
Files to Review
Documentation
- Create a React Dashboard Application
- Dashboard Component for React
- Install DevExpress Controls Using NuGet Packages
Examples
- Dashboard for React - Configuration
- Dashboard for Angular - Get Started
- Dashboard for Vue - Get Started
- Dashboard for ASP.NET Core - Backend
Does this example address your development requirements/objectives?
(you will be redirected to DevExpress.com to submit your response)
Example Code
Codeimport React from 'react';
import 'ace-builds/css/ace.css';
import 'ace-builds/css/theme/dreamweaver.css';
import 'ace-builds/css/theme/ambiance.css';
import 'devextreme/dist/css/dx.light.css';
import '@devexpress/analytics-core/dist/css/dx-analytics.common.css';
import '@devexpress/analytics-core/dist/css/dx-analytics.light.css';
import '@devexpress/analytics-core/dist/css/dx-querybuilder.css';
import 'devexpress-dashboard/dist/css/dx-dashboard.light.css';
import DashboardControl from 'devexpress-dashboard-react';
function App() {
return (
<div style={{ position : 'absolute', top : '0px', left: '0px', right : '0px', bottom: '0px' }}>
<DashboardControl style={{ height: '100%' }}
endpoint="http://localhost:5000/api/dashboard">
</DashboardControl>
</div>
);
}
export default App;
C#using DevExpress.DashboardAspNetCore;
using DevExpress.DashboardWeb.Native;
using DevExpress.DashboardWeb;
using Microsoft.Extensions.FileProviders;
using DevExpress.AspNetCore;
using DevExpress.DashboardCommon;
using DevExpress.DataAccess.Json;
var builder = WebApplication.CreateBuilder(args);
IFileProvider? fileProvider = builder.Environment.ContentRootFileProvider;
IConfiguration? configuration = builder.Configuration;
// Configures CORS policies.
builder.Services.AddCors(options => {
options.AddPolicy("CorsPolicy", builder => {
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.WithHeaders("Content-Type");
});
});
// Adds the DevExpress middleware.
builder.Services.AddDevExpressControls();
// Adds controllers.
builder.Services.AddControllersWithViews();
// Configures the dashboard backend.
builder.Services.AddScoped<DashboardConfigurator>((IServiceProvider serviceProvider) => {
DashboardConfigurator configurator = new DashboardConfigurator();
configurator.SetDashboardStorage(new DashboardFileStorage(fileProvider.GetFileInfo("Data/Dashboards").PhysicalPath));
configurator.SetDataSourceStorage(CreateDataSourceStorage());
configurator.SetConnectionStringsProvider(new DashboardConnectionStringsProvider(configuration));
configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
return configurator;
});
void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.ConnectionName == "jsonSupport") {
Uri fileUri = new Uri(fileProvider.GetFileInfo("Data/support.json").PhysicalPath, UriKind.RelativeOrAbsolute);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
if (e.ConnectionName == "jsonCategories") {
Uri fileUri = new Uri(fileProvider.GetFileInfo("Data/categories.json").PhysicalPath, UriKind.RelativeOrAbsolute);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
}
DataSourceInMemoryStorage CreateDataSourceStorage() {
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
DashboardJsonDataSource jsonDataSourceSupport = new DashboardJsonDataSource("Support");
jsonDataSourceSupport.ConnectionName = "jsonSupport";
jsonDataSourceSupport.RootElement = "Employee";
dataSourceStorage.RegisterDataSource("jsonDataSourceSupport", jsonDataSourceSupport.SaveToXml());
DashboardJsonDataSource jsonDataSourceCategories = new DashboardJsonDataSource("Categories");
jsonDataSourceCategories.ConnectionName = "jsonCategories";
jsonDataSourceCategories.RootElement = "Products";
dataSourceStorage.RegisterDataSource("jsonDataSourceCategories", jsonDataSourceCategories.SaveToXml());
return dataSourceStorage;
}
var app = builder.Build();
// Registers the DevExpress middleware.
app.UseDevExpressControls();
// Registers routing.
app.UseRouting();
// Registers CORS policies.
app.UseCors("CorsPolicy");
// Maps the dashboard route.
app.MapDashboardRoute("api/dashboard", "DefaultDashboard");
// Requires CORS policies.
app.MapControllers().RequireCors("CorsPolicy");
app.Run();