1
- using System ;
2
- using System . Collections . Generic ;
3
- using System . Linq ;
4
- using System . Threading . Tasks ;
5
- using Microsoft . AspNetCore . Hosting ;
6
- using Microsoft . Extensions . Configuration ;
7
- using Microsoft . Extensions . Hosting ;
8
- using Microsoft . Extensions . Logging ;
9
-
10
- namespace AspNetCoreDashboardBackend {
11
- public class Program {
12
- public static void Main ( string [ ] args ) {
13
- CreateHostBuilder ( args ) . Build ( ) . Run ( ) ;
14
- }
15
-
16
- public static IHostBuilder CreateHostBuilder ( string [ ] args ) =>
17
- Host . CreateDefaultBuilder ( args )
18
- . ConfigureWebHostDefaults ( webBuilder => {
19
- webBuilder . UseStartup < Startup > ( ) ;
20
- } ) ;
1
+ using DevExpress . DashboardAspNetCore ;
2
+ using DevExpress . DashboardWeb . Native ;
3
+ using DevExpress . DashboardWeb ;
4
+ using Microsoft . Extensions . FileProviders ;
5
+ using DevExpress . AspNetCore ;
6
+ using DevExpress . DashboardCommon ;
7
+ using DevExpress . DataAccess . Json ;
8
+
9
+ var builder = WebApplication . CreateBuilder ( args ) ;
10
+
11
+ IFileProvider ? fileProvider = builder . Environment . ContentRootFileProvider ;
12
+ IConfiguration ? configuration = builder . Configuration ;
13
+
14
+ // Configures CORS policies.
15
+ builder . Services . AddCors ( options => {
16
+ options . AddPolicy ( "CorsPolicy" , builder => {
17
+ builder . AllowAnyOrigin ( ) ;
18
+ builder . AllowAnyMethod ( ) ;
19
+ builder . WithHeaders ( "Content-Type" ) ;
20
+ } ) ;
21
+ } ) ;
22
+
23
+ // Adds the DevExpress middleware.
24
+ builder . Services . AddDevExpressControls ( ) ;
25
+ // Adds controllers.
26
+ builder . Services . AddControllersWithViews ( ) ;
27
+
28
+ // Configures the dashboard backend.
29
+ builder . Services . AddScoped < DashboardConfigurator > ( ( IServiceProvider serviceProvider ) => {
30
+ DashboardConfigurator configurator = new DashboardConfigurator ( ) ;
31
+ configurator . SetDashboardStorage ( new DashboardFileStorage ( fileProvider . GetFileInfo ( "Data/Dashboards" ) . PhysicalPath ) ) ;
32
+ configurator . SetDataSourceStorage ( CreateDataSourceStorage ( ) ) ;
33
+ configurator . SetConnectionStringsProvider ( new DashboardConnectionStringsProvider ( configuration ) ) ;
34
+ configurator . ConfigureDataConnection += Configurator_ConfigureDataConnection ;
35
+ return configurator ;
36
+ } ) ;
37
+
38
+ void Configurator_ConfigureDataConnection ( object sender , ConfigureDataConnectionWebEventArgs e ) {
39
+ if ( e . ConnectionName == "jsonSupport" ) {
40
+ Uri fileUri = new Uri ( fileProvider . GetFileInfo ( "Data/support.json" ) . PhysicalPath , UriKind . RelativeOrAbsolute ) ;
41
+ JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters ( ) ;
42
+ jsonParams . JsonSource = new UriJsonSource ( fileUri ) ;
43
+ e . ConnectionParameters = jsonParams ;
21
44
}
45
+ if ( e . ConnectionName == "jsonCategories" ) {
46
+ Uri fileUri = new Uri ( fileProvider . GetFileInfo ( "Data/categories.json" ) . PhysicalPath , UriKind . RelativeOrAbsolute ) ;
47
+ JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters ( ) ;
48
+ jsonParams . JsonSource = new UriJsonSource ( fileUri ) ;
49
+ e . ConnectionParameters = jsonParams ;
50
+ }
51
+ }
52
+
53
+ DataSourceInMemoryStorage CreateDataSourceStorage ( ) {
54
+ DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage ( ) ;
55
+
56
+ DashboardJsonDataSource jsonDataSourceSupport = new DashboardJsonDataSource ( "Support" ) ;
57
+ jsonDataSourceSupport . ConnectionName = "jsonSupport" ;
58
+ jsonDataSourceSupport . RootElement = "Employee" ;
59
+ dataSourceStorage . RegisterDataSource ( "jsonDataSourceSupport" , jsonDataSourceSupport . SaveToXml ( ) ) ;
60
+
61
+ DashboardJsonDataSource jsonDataSourceCategories = new DashboardJsonDataSource ( "Categories" ) ;
62
+ jsonDataSourceCategories . ConnectionName = "jsonCategories" ;
63
+ jsonDataSourceCategories . RootElement = "Products" ;
64
+ dataSourceStorage . RegisterDataSource ( "jsonDataSourceCategories" , jsonDataSourceCategories . SaveToXml ( ) ) ;
65
+ return dataSourceStorage ;
22
66
}
67
+
68
+ var app = builder . Build ( ) ;
69
+
70
+ // Registers the DevExpress middleware.
71
+ app . UseDevExpressControls ( ) ;
72
+
73
+ // Registers routing.
74
+ app . UseRouting ( ) ;
75
+ // Registers CORS policies.
76
+ app . UseCors ( "CorsPolicy" ) ;
77
+
78
+ // Maps the dashboard route.
79
+ app . MapDashboardRoute ( "api/dashboard" , "DefaultDashboard" ) ;
80
+ // Requires CORS policies.
81
+ app . MapControllers ( ) . RequireCors ( "CorsPolicy" ) ;
82
+
83
+ app . Run ( ) ;
0 commit comments