关于angular:Mark企业级Angular-Dashboard应用程序创建Part-2

32次阅读

共计 3179 个字符,预计需要花费 8 分钟才能阅读完成。

点击获取工具 >>

重要提醒 :应用本教程须要相熟 React 的基本概念和模式,要查看这些概念,请拜访 angular.io。

Step 2. 创立服务器应用程序

创立一个自定义服务器应用程序来显示您的数据,请按以下步骤操作:

  1. 在 Visual Studio 中,创立一个 ASP.NET Core 3.1 应用程序,抉择 Empty template。
  2. 创立将存储仪表板的 App_Data / Dashboards 文件夹。
  3. 用以下代码替换 Startup.cs 文件的内容:

`using DevExpress.AspNetCore;
using DevExpress.DashboardAspNetCore;
using DevExpress.DashboardCommon;
using DevExpress.DashboardWeb;
using DevExpress.DataAccess.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.FileProviders;
using System;

namespace AspNetCoreDashboardBackend {
public class Startup {
public Startup(IConfiguration configuration, IWebHostEnvironment hostingEnvironment) {
Configuration = configuration;
FileProvider = hostingEnvironment.ContentRootFileProvider;
}

public IConfiguration Configuration {get;}
public IFileProvider FileProvider {get;}

public void ConfigureServices(IServiceCollection services) {
services
// Configures CORS policies.
.AddCors(options => {
options.AddPolicy(“CorsPolicy”, builder => {
builder.AllowAnyOrigin();
builder.AllowAnyMethod();
builder.WithHeaders(“Content-Type”);
});
})
// Adds the DevExpress middleware.
.AddDevExpressControls()
// Adds controllers.
.AddControllers()
// Configures the dashboard backend.
.AddDefaultDashboardController(configurator => {
configurator.SetDashboardStorage(new DashboardFileStorage(FileProvider.GetFileInfo(“App_Data/Dashboards”).PhysicalPath));
configurator.SetDataSourceStorage(CreateDataSourceStorage());
configurator.ConfigureDataConnection += Configurator_ConfigureDataConnection;
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
// Registers the DevExpress middleware.
app.UseDevExpressControls();
// Registers routing.
app.UseRouting();
// Registers CORS policies.
app.UseCors(“CorsPolicy”);
app.UseEndpoints(endpoints => {
// Maps the dashboard route.
EndpointRouteBuilderExtension.MapDashboardRoute(endpoints, “api/dashboard”);
// Requires CORS policies.
endpoints.MapControllers().RequireCors(“CorsPolicy”);
});
}
public DataSourceInMemoryStorage CreateDataSourceStorage() {
DataSourceInMemoryStorage dataSourceStorage = new DataSourceInMemoryStorage();
DashboardJsonDataSource jsonDataSource = new DashboardJsonDataSource(“Customers”);
jsonDataSource.RootElement = “Customers”;
dataSourceStorage.RegisterDataSource(“jsonDataSourceSupport”, jsonDataSource.SaveToXml());
return dataSourceStorage;
}
private void Configurator_ConfigureDataConnection(object sender, ConfigureDataConnectionWebEventArgs e) {
if (e.DataSourceName.Contains(“Customers”)) {
Uri fileUri = new Uri(“https://raw.githubusercontent.com/DevExpress-Examples/DataSources/master/JSON/customers.json”);
JsonSourceConnectionParameters jsonParams = new JsonSourceConnectionParameters();
jsonParams.JsonSource = new UriJsonSource(fileUri);
e.ConnectionParameters = jsonParams;
}
}
}
}
`

  1. 运行以下命令来启动服务器:

cmd

dotnet run

  1. 要在客户端应用程序中应用此服务器,请跳转到 app.component.html 文件。将以下 URL 设置为端点:http:// localhost:5000 / api / dashboard

html

`<dx-dashboard-control

endpoint=’http://localhost:5000/api/dashboard’>
</dx-dashboard-control>`

Step 3. 切换到 Viewer 模式

创立并保留仪表板后,您能够将仪表板设计器切换到 Viewer 模式。

  1. 关上 app.component.html 文件,并将 workingMode 属性设置为 ViewerOnly:

html

`<dx-dashboard-control

endpoint=’http://localhost:5000/api/dashboard’
workingMode=’ViewerOnly’>
</dx-dashboard-control>
`

正文完
 0