我正在使用 HTML 的以下代码(呈现部分)方法在我的网页上呈现多个 Power Bi 报告。
调试后,我发现代码可以正常工作,结果存储不同的 ID、嵌入 URL 和相应报告的 URL。但是在网页上加载之后,我得到了一个 Power Bi 加载符号,之后所有的图块都显示了最后的报告。尽管 's 正在获取不同的报告,但这些图块并未显示不同的报告。
这是代码(该代码可以正常存储所有不同的报告),但是当 Power Bi 引擎在网页上加载时,我只得到所有图块中的最后一个报告:
控制器:
public class TelemetryController : Controller
{
private string workspaceCollection;
private Guid workspaceId;
private string signingKey;
private string apiUrl;
public TelemetryReports telemetryreport;
public TelemetryController()
{
this.workspaceCollection = ConfigurationManager.AppSettings["powerbi:WorkspaceCollection"];
this.workspaceId = Guid.Parse(ConfigurationManager.AppSettings["powerbi:WorkspaceId"]);
this.signingKey = ConfigurationManager.AppSettings["powerbi:SigningKey"];
this.apiUrl = ConfigurationManager.AppSettings["powerbi:ApiUrl"];
}
// GET: Telemetry
public ActionResult TelemetryIndex()
{
var authResponse = new SecurityHelper().Authenticate(new AuthenticateRequest() { IsNonMRT = false, isOnLoad = true });
telemetryreport = new TelemetryReports();
if (!authResponse.IsAuthenticated)
throw new NotAuthorizedException((String.Format(ResourceMessages.GetErrorMessage("REW_ERR_0005"), authResponse.context.LoggedInAlias)) + ";" + ((int)PAFEventID.REW_ERR_0005).ToString());
var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId.ToString());
using (var client = this.CreatePowerBIClient(devToken))
{
var reportsResponse = client.Reports.GetReports(this.workspaceCollection, this.workspaceId.ToString());
foreach (var item in reportsResponse.Value)
{
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId.ToString(), Guid.Parse(item.Id).ToString());
TelemetryReports.ReportViewModel _report = new TelemetryReports.ReportViewModel()
{
Report = item,
AccessToken = embedToken.Generate(this.signingKey)
};
telemetryreport.Reports.Add(_report);
}
}
return View(telemetryreport);
}
[ChildActionOnly]
public ActionResult Reports()
{
var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId.ToString());
using (var client = this.CreatePowerBIClient(devToken))
{
var reportsResponse = client.Reports.GetReports(this.workspaceCollection, this.workspaceId.ToString());
var viewModel = new TelemetryReports.ReportsViewModel
{
Reports = reportsResponse.Value.ToList()
};
return PartialView(viewModel);
}
}
public async Task<ActionResult> Report(string reportId)
{
var devToken = PowerBIToken.CreateDevToken(this.workspaceCollection, this.workspaceId.ToString());
using (var client = this.CreatePowerBIClient(devToken))
{
var reportsResponse = await client.Reports.GetReportsAsync(this.workspaceCollection, this.workspaceId.ToString());
var report = reportsResponse.Value.FirstOrDefault(r => r.Id == reportId);
var embedToken = PowerBIToken.CreateReportEmbedToken(this.workspaceCollection, this.workspaceId.ToString(), report.Id);
var viewModel = new TelemetryReports.ReportViewModel
{
Report = report,
AccessToken = embedToken.Generate(this.signingKey)
};
return View(viewModel);
}
}
private IPowerBIClient CreatePowerBIClient(PowerBIToken token)
{
var jwt = token.Generate(signingKey);
var credentials = new TokenCredentials(jwt, "AppToken");
var client = new PowerBIClient(credentials)
{
BaseUri = new Uri(apiUrl)
};
return client;
}
}
TelemetryReport.cs(以防调试需要)
public class TelemetryReports
{
public TelemetryReports() {
Reports = new List<ReportViewModel>();
}
public List<ReportViewModel> Reports { get; set; }
public class ReportsViewModel
{
public List<Report> Reports { get; set; }
}
public class ReportViewModel
{
public IReport Report { get; set; }
public string AccessToken { get; set; }
}
}
public class TelemetryReport
{
public IReport Report { get; set; }
public string AccessToken { get; set; }
}
索引.HTML文件:
<!DOCTYPE html>
<html lang="en">
<head>
@Styles.Render("~/Content/telemetry")
@{
Layout = null;
}
@model TelemetryReports
<script type="text/javascript" src="/js/app.js"></script>
<script src="~/Scripts/app/powerbi.js"></script>
<script src="~/Scripts/lib/chart.js"></script>
<script src="~/Scripts/lib/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/lib/bootstrap.min.js"></script>
</head>
<body>
<section>
<nav class="cl-effect-1" style="background-color:transparent">
<a class="fifth before after" href="/">Home</a>
</nav>
</section>
<br />
<div class="TelemetryReport">
@for (int i = 0; i < Model.Reports.Count; i++)
{
@Html.Partial("Report", Model.Reports[i])
}
</div>
</body>
</html>
报告.HTML:
@using Microsoft.PowerBI.AspNet.Mvc;
@{
Layout = "";
}
@model TelemetryReports.ReportViewModel
<body>
<section class="color-9">
<nav class="cl-effect-13">
<div class="active">@Model.Report.Name</div>
</nav>
</section>
@Html.PowerBIAccessToken(Model.AccessToken)
@Html.PowerBIReport(Model.Report.Name, Model.Report.EmbedUrl, new { style = "height:35vh" })
@*<div>
@Html.PowerBIAccessToken(Model.AccessToken)
@Html.PowerBIReport(Model.Report.Name,Model.Report.EmbedUrl, new { style = "height:85vh ; width:65vh;" })
</div>*@
</body>
最终的网页如下所示: