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中创建API控制器用于提供GoView所需数据:
// Controllers/GoViewController.csusingMicrosoft.AspNetCore.Mvc;namespaceGoViewDemo.Controllers;[ApiController][Route("api/[controller]")]publicclassGoViewController:ControllerBase{[HttpGet("chartData")]publicIActionResultGetChartData(){var data =new{ categories =new[]{"周一","周二","周三","周四","周五","周六","周日"}, series =new[]{new{ name ="邮件营销", data =new[]{120,132,101,134,90,230,210}},new{ name ="联盟广告", data =new[]{220,182,191,234,290,330,310}}}};returnOk(data);}}
7. 修改GoView配置
编辑 wwwroot/js/app.*.js 文件,修改API请求地址:
axios.defaults.baseURL ='/api';8. 运行项目 bash dotnet run
[HttpGet("themes")]publicIActionResultGetThemes(){var themes =new[]{new{ id ="default", name ="默认主题"},new{ id ="dark", name ="暗黑主题"},new{ id ="light", name ="明亮主题"}};returnOk(themes);}[HttpPost("setTheme/{themeId}")]publicIActionResultSetTheme(string themeId){// 这里可以实现主题切换逻辑returnOk(new{ message =$"主题已切换为{themeId}"});}