.NET集成GoView低代碼實現(xiàn)可視化大屏完整案例詳解
一、GoView簡介
GoView 是一款基于 Vue3.x 構(gòu)建的低代碼數(shù)據(jù)可視化開發(fā)平臺,它允許開發(fā)者通過簡單的配置快速構(gòu)建各種數(shù)據(jù)可視化大屏。
- 官網(wǎng)文檔:https://mtruning.club/
- 純前端 Demo 地址:https://vue.mtruning.club/
- 帶后端 Demo 地址:https://demo.mtruning.club/
- GoView 源碼地址:https://gitee.com/MTrun/go-view
- JAVA https://gitee.com/MTrun/go-view-serve (當前使用)
- .NET https://gitee.com/sun_xiang_yu/go-view-dotnet
GoView 具有以下特點:
- 低代碼開發(fā):通過拖拽組件和配置屬性即可完成大屏開發(fā)
- 豐富的組件庫:內(nèi)置多種圖表、地圖、表格等常用組件
- 響應(yīng)式設(shè)計:適配不同屏幕尺寸
- 數(shù)據(jù)驅(qū)動:支持動態(tài)數(shù)據(jù)綁定和實時更新
- 主題定制:可自定義主題顏色和樣式
GoView 特別適合企業(yè)級數(shù)據(jù)可視化需求,如運營監(jiān)控大屏、數(shù)據(jù)分析看板、指揮中心大屏等場景。
二、.NET集成GoView方案
在 .NET 項目中集成 GoView 通常有兩種方式:
- 前后端分離:.NET作為后端API服務(wù),GoView作為獨立前端項目
- 嵌入式集成:將GoView打包后嵌入到.NET MVC或Razor Pages中
本文將重點介紹第二種方式,實現(xiàn)GoView與.NET的無縫集成。
三、集成步驟詳解
1. 環(huán)境準備
.NET 6+開發(fā)環(huán)境Node.js環(huán)境(用于構(gòu)建GoView前端)- GoView源碼(可從GitHub獲?。?/li>
2. 獲取并構(gòu)建GoView
# 克隆GoView倉庫 git clone https://gitee.com/dromara/go-view.git # 進入項目目錄 cd go-view # 安裝依賴 npm install # 構(gòu)建生產(chǎn)版本 npm run build
構(gòu)建完成后,會在項目目錄下生成dist文件夾,包含所有靜態(tài)資源。
3. 創(chuàng)建.NET項目
dotnet new webapp -n GoViewDemo cd GoViewDemo
集成GoView靜態(tài)資源
將GoView的 dist 文件夾內(nèi)容復(fù)制到.NET項目的 wwwroot 目錄下:
wwwroot/
├─ css/
├─ js/
├─ img/
├─ favicon.ico
└─ index.html
5. 修改.NET路由配置
在 Program.cs 中添加靜態(tài)文件服務(wù)和重定向:
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
// 添加GoView路由
app.MapGet("/", () => Results.Redirect("/index.html"));
app.MapRazorPages();
app.Run();
6. 配置API接口
在.NET中創(chuàng)建API控制器用于提供GoView所需數(shù)據(jù):
// Controllers/GoViewController.cs
using Microsoft.AspNetCore.Mvc;
namespace GoViewDemo.Controllers;
[ApiController]
[Route("api/[controller]")]
public class GoViewController : ControllerBase
{
[HttpGet("chartData")]
public IActionResult GetChartData()
{
var data = new
{
categories = new[] { "周一", "周二", "周三", "周四", "周五", "周六", "周日" },
series = new[]
{
new { name = "郵件營銷", data = new[] { 120, 132, 101, 134, 90, 230, 210 } },
new { name = "聯(lián)盟廣告", data = new[] { 220, 182, 191, 234, 290, 330, 310 } }
}
};
return Ok(data);
}
}
7. 修改GoView配置
編輯 wwwroot/js/app.*.js 文件,修改API請求地址:
axios.defaults.baseURL = '/api';
8. 運行項目
dotnet run
訪問 https://localhost:5001 即可看到集成的GoView大屏。
四、進階集成方案
1. 身份驗證集成
在.NET中添加JWT認證,并在GoView中配置請求攔截器:
// Program.cs
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(options =>
{
options.TokenValidationParameters = new TokenValidationParameters
{
ValidateIssuer = true,
ValidateAudience = true,
ValidateLifetime = true,
ValidateIssuerSigningKey = true,
ValidIssuer = builder.Configuration["Jwt:Issuer"],
ValidAudience = builder.Configuration["Jwt:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(
Encoding.UTF8.GetBytes(builder.Configuration["Jwt:Key"]))
};
});
在GoView中添加請求攔截器:
// 在main.js或axios配置文件中
axios.interceptors.request.use(config => {
const token = localStorage.getItem('token');
if (token) {
config.headers.Authorization = `Bearer ${token}`;
}
return config;
}, error => {
return Promise.reject(error);
});
2. 動態(tài)主題切換
在.NET中創(chuàng)建主題API:
[HttpGet("themes")]
public IActionResult GetThemes()
{
var themes = new[]
{
new { id = "default", name = "默認主題" },
new { id = "dark", name = "暗黑主題" },
new { id = "light", name = "明亮主題" }
};
return Ok(themes);
}
[HttpPost("setTheme/{themeId}")]
public IActionResult SetTheme(string themeId)
{
// 這里可以實現(xiàn)主題切換邏輯
return Ok(new { message = $"主題已切換為{themeId}" });
}
在GoView中添加主題切換組件并調(diào)用API。
3. 數(shù)據(jù)緩存優(yōu)化
使用.NET的 MemoryCache 優(yōu)化數(shù)據(jù)查詢:
[HttpGet("cachedData")]
public async Task<IActionResult> GetCachedData([FromServices] IMemoryCache cache)
{
const string cacheKey = "chart_data";
if (!cache.TryGetValue(cacheKey, out var data))
{
// 模擬從數(shù)據(jù)庫獲取數(shù)據(jù)
data = await FetchDataFromDatabase();
// 設(shè)置緩存選項
var cacheOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromMinutes(5));
cache.Set(cacheKey, data, cacheOptions);
}
return Ok(data);
}
五、常見問題解決
1.跨域問題
在開發(fā)環(huán)境中配置CORS:
builder.Services.AddCors(options =>
{
options.AddPolicy("AllowAll", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
});
app.UseCors("AllowAll");
2. 靜態(tài)文件404錯誤
- 確保
UseStaticFiles在中間件管道中的正確位置 - 檢查文件路徑和大小寫是否正確
3. API請求路徑問題
- 確保
GoView中配置的API路徑與.NET路由匹配 - 使用相對路徑而不是絕對路徑
4. 性能優(yōu)化
啟用響應(yīng)壓縮
builder.Services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
});
app.UseResponseCompression();
六、總結(jié)
通過本文的介紹,我們了解了如何在.NET項目中完整集成GoView數(shù)據(jù)可視化平臺。這種集成方式既保留了GoView強大的可視化能力,又可以利用.NET的穩(wěn)定性和安全性構(gòu)建企業(yè)級應(yīng)用。關(guān)鍵點包括:
- 正確構(gòu)建和部署GoView靜態(tài)資源
- 合理設(shè)計API接口滿足數(shù)據(jù)需求
- 處理身份驗證和安全問題
- 優(yōu)化性能和用戶體驗
這種集成方案特別適合需要將數(shù)據(jù)可視化功能嵌入到現(xiàn)有.NET應(yīng)用中的場景,如企業(yè)內(nèi)部管理系統(tǒng)、數(shù)據(jù)監(jiān)控平臺等。開發(fā)者可以根據(jù)實際需求進一步擴展和定制,構(gòu)建更加強大和個性化的數(shù)據(jù)可視化解決方案。
到此這篇關(guān)于.NET集成GoView低代碼實現(xiàn)可視化大屏完整案例詳解的文章就介紹到這了,更多相關(guān).NET GoView可視化大屏內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Visual Studio 2017 15.5 正式發(fā)布!性能再提升
Visual Studio 2017 15.5 正式發(fā)布!性能再提升,時發(fā)布的還有 Visual Studio for Mac 7.3,亮點如下2017-12-12
ASP.NET配合jQuery解決跨域調(diào)用的問題
這篇文章主要介紹了ASP.NET配合jQuery解決跨域調(diào)用的問題,簡單實用,需要的朋友可以參考下。2016-06-06
ASP.NET編程簡單實現(xiàn)生成靜態(tài)頁面的方法【附demo源碼下載】
這篇文章主要介紹了ASP.NET編程簡單實現(xiàn)生成靜態(tài)頁面的方法,較為詳細的分析了asp.net生成靜態(tài)頁面的步驟與相關(guān)操作技巧,并附帶相關(guān)實例源碼供讀者下載參考,需要的朋友可以參考下2017-07-07
Asp.net實現(xiàn)選擇性的保留DataTable中的列
選擇性的保留DataTable中的列(移除列/保留列不移除/移除不需要的列),很多新手朋友們都想實現(xiàn)這樣的功能,本文總結(jié)了一些可行方法,感興趣的朋友可以了解下哦2013-01-01
Asp.net利用一般處理程序?qū)崿F(xiàn)文件下載功能
這篇文章主要介紹了Asp.net利用一般處理程序?qū)崿F(xiàn)文件下載功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-07-07
asp.net中簡體轉(zhuǎn)繁體實現(xiàn)代碼
最近到了臺企,什么都要用繁體的。開發(fā)中也遇到了簡繁體轉(zhuǎn)換的問題。這里和朋友們分享一下用.net實現(xiàn)簡繁體轉(zhuǎn)換的經(jīng)驗。2010-03-03

