JSON Data Source in an ASP.NET MVC App
- 4 minutes to read
This tutorial shows how to add the DashboardJsonDataSource to an in-memory data source storage, and make it available to users. The tutorial uses an online resource, a file, and a JSON string.
Important
The DashboardJsonDataSource
object requires the System.Text.Json library. .NET projects do not require manual installation of the System.Text.Json package, as it is already included in the .NET environment. Set the DevExpress.DataAccess.Native.Json.JsonLoaderHelper.ProcessingLibrary
property to NewtonsoftJson
to use the Newtonsoft.Json library instead. Then, install the Newtonsoft.Json NuGet package.
In the dashboard configuration file (for example, DashboardConfig.cs / DashboardConfig.vb), create a public method that returns the configured dashboard’s data source storage (DataSourceInMemoryStorage) and define the JSON data sources. Use the JsonDataSource.RootElement property to specify the name of the root element in the associated data source. Specify JsonDataSource.JsonSource, which stores JSON data import settings, and set JsonDataSource.ConnectionName for a data source that obtains data from a JSON file.
using System; using DevExpress.DashboardCommon; using DevExpress.DashboardWeb; using DevExpress.DataAccess.Json; public static DataSourceInMemoryStorage CreateDataSourceStorage() { DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage(); // Creates a JSON data source from a URL. DashboardJsonDataSource jsonDataSourceUrl = new DashboardJsonDataSource("JSON Data Source (URL)"); jsonDataSourceUrl.JsonSource = new UriJsonSource(new Uri("https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json")); jsonDataSourceUrl.RootElement = "Customers"; dataSourceStorage.RegisterDataSource("jsonDataSourceUrl", jsonDataSourceUrl.SaveToXml()); // Creates a JSON data source from a JSON file. DashboardJsonDataSource jsonDataSourceFile = new DashboardJsonDataSource("JSON Data Source (File)"); jsonDataSourceFile.ConnectionName = "jsonConnection"; jsonDataSourceFile.RootElement = "Customers"; dataSourceStorage.RegisterDataSource("jsonDataSourceFile", jsonDataSourceFile.SaveToXml()); // Uses a JSON string to create a JSON data source. DashboardJsonDataSource jsonDataSourceString = new DashboardJsonDataSource("JSON Data Source (String)"); string json = "{\"Customers\":[{\"Id\":\"ALFKI\",\"CompanyName\":\"Alfreds Futterkiste\",\"ContactName\":\"Maria Anders\",\"ContactTitle\":\"Sales Representative\",\"Address\":\"Obere Str. 57\",\"City\":\"Berlin\",\"PostalCode\":\"12209\",\"Country\":\"Germany\",\"Phone\":\"030-0074321\",\"Fax\":\"030-0076545\"}],\"ResponseStatus\":{}}"; jsonDataSourceString.JsonSource = new CustomJsonSource(json); jsonDataSourceString.RootElement = "Customers"; dataSourceStorage.RegisterDataSource("jsonDataSourceString", jsonDataSourceString.SaveToXml()); return dataSourceStorage; }
Call the DashboardConfigurator.SetDataSourceStorage method to configure the data source storage. Use the created CreateDataSourceStorage method as the SetDataSourceStorage parameter. Then handle the DashboardConfigurator.ConfigureDataConnection event to pass the connection parameters to the JSON data source.
using System; using DevExpress.DashboardWeb; using DevExpress.DataAccess.Json; public static void RegisterService(RouteCollection routes) { routes.MapDashboardRoute("dashboardControl", "DefaultDashboard"); // ... DashboardConfigurator.Default.SetDataSourceStorage(CreateDataSourceStorage()); DashboardConfigurator.Default.ConfigureDataConnection += Default_ConfigureDataConnection; } private static void Default_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) { if(e.ConnectionName == "jsonConnection") { Uri fileUri = new Uri(HostingEnvironment.MapPath(@"~/App_Data/customers.json"), UriKind.RelativeOrAbsolute); JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters(); jsonParams.JsonSource = new UriJsonSource(fileUri); e.ConnectionParameters = jsonParams; } }
The JSON data sources are now available in the Web Dashboard:
Users can now bind dashboard items to data in the Web Dashboard’s UI.
Dashboard Data Source Wizard
Users can use the Dashboard Data Source Wizard to create a new dashboard data source based on JSON data. To store new data connections, you need to implement the JSON data connection storage.
See the following topic for details: Specify Data Source Settings (JSON).
Example
The example shows how to make a set of data sources available for users in the Web Dashboard application.