Supply Custom Headers for JSON Database
Unsere Beispiele der Projekte und Berichtsvorlagen helfen Ihnen, die Grundlagen der Arbeit mit unserer Software zu erlernen.Dieses Beispiel ist veraltet, checken Sie viele anderen aktualisierten Beispiele in dieser Kategorie aus.This example shows how to supply custom headers for JSON Database. First, create the new dashboard and load file:
Then, in
After that, adjust database PathData to always link to the server:
Next, show dashboard in the viewer:
Use
Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen:

function onSelectReport() {
// Create a new dashboard instance and load dashboard from server
var report = Stimulsoft.Report.StiReport.createNewDashboard();
report.loadFile("/GetDashboard");
...
Then, in
onBeginProcessData event handler add custom HTTP headers:
...
// In `onBeginProcessData` event handler add custom HTTP headers
report.onBeginProcessData = args => {
if (args.database === "JSON" && args.command === "GetData" && args.pathData && args.pathData.endsWith("/GetJson")) {
// Add custom header to pass through server protection
args.headers.push({ key: "X-Auth-Token", value: "*YOUR TOKEN*" });
}
};
...
After that, adjust database PathData to always link to the server:
...
// Adjust database PathData to always link to our server
report.dictionary.databases.getByIndex(0).pathData = `${window.location.origin}/GetJson`;
...
Next, show dashboard in the viewer:
...
// View dashboard in Viewer
var viewer = new Stimulsoft.Viewer.StiViewer(null, "StiViewer", false);
viewer.report = report;
viewer.renderHtml("viewer");
}
onSelectReport();
Use
Configure() method to configure the HTTP request pipeline:
...
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
endpoints.MapGet("/GetDashboard", async context =>
{
if (await SendJsonFile(context, env, "DBSsimp.mrt")) return;
context.Response.StatusCode = 404;
});
endpoints.MapGet("/GetJson", async context =>
{
if (context.Request.Headers["x-auth-token"] == "*YOUR TOKEN*")
{
if (await SendJsonFile(context, env, "ProtectedDemo.json")) return;
}
context.Response.StatusCode = 403;
});
});
}
private async Task<bool> SendJsonFile(HttpContext context, IWebHostEnvironment env, string FileName)
{
var fileInfo = env.WebRootFileProvider.GetFileInfo(string.Format("Dashboards/{0}", FileName));
if (fileInfo.Exists)
{
context.Response.Headers.Add("Content-Type", "application/json; charset=utf-8");
using (var reportStream = fileInfo.CreateReadStream())
{
await reportStream.CopyToAsync(context.Response.Body);
}
await context.Response.CompleteAsync();
return true;
}
return false;
}Auf dem Screenshot unten Sie können das Ergebnis des Beispiel-Codes ansehen:
