Blazor Web Assembly
App applicationBold Reports JavaScript ReportViewer can be used with Blazor applications using ASP.NET Core Blazor JavaScript interoperability. Refer to the following steps to use Bold Reports JavaScript ReportViewer with Blazor Web Assembly App template application.
ReportViewer requires a Web API service for report processing. So, you have to create the Blazor Web Assembly App along with ASP.NET Core Hosted configuration to have the ASP.NET Core back end with Blazor application itself.
wwwroot\index.html
.<!-- Report Viewer component styles -->
<link href="https://cdn.boldreports.com/6.3.24/content/v2.0/tailwind-light/bold.report-viewer.min.css" rel="stylesheet" />
<!-- Report Viewer component dependent script -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.boldreports.com/6.3.24/scripts/v2.0/common/bold.reports.common.min.js"></script>
<script src="https://cdn.boldreports.com/6.3.24/scripts/v2.0/common/bold.reports.widgets.min.js"></script>
<!-- Report Viewer component script -->
<script src="https://cdn.boldreports.com/6.3.24/scripts/v2.0/bold.report-viewer.min.js"></script>
index.html
to add our JavaScript ReportViewer with element using jQuery. This will be invoked using IJSRuntime from razor pages along with name of the report and element need to be created with ReportViewer.<script>
function RenderReportViewer (reportPathInfo, elementID) {
$("#"+ elementID).boldReportViewer({
reportServiceUrl: '/api/ReportApi',
reportPath: reportPathInfo
});
};
</script>
@page "/reporting"
@using Microsoft.JSInterop
@using Microsoft.AspNetCore.Components
@inject IJSRuntime JSRuntime
<div id="reportViewerControl" style="width: 100%;height:800px" />
@code {
private string reportPathInput = "Report1";
// You have to call this method based on your need. It can called while loading the page or after selection the report.
public async void RenderReport()
{
await JSRuntime.InvokeVoidAsync("RenderReportViewer", reportPathInput , "reportViewerControl");
}
/// https://docs.microsoft.com/en-us/aspnet/core/blazor-reporting/components?view=aspnetcore-3.0
/// https://github.com/aspnet/AspNetCore.Docs/tree/master/aspnetcore/blazor-reporting/common/samples/3.x/BlazorSample
protected override void OnAfterRender(bool firstRender)
{
// If you want to load in intialization you can make use of this.
//RenderReport();
}
}
Refer ASP.NET Core Web API Service
You can ignore Enable Cross origin topic with documentation. since, the service with Blazor application and there is no need to enable cores.
[Route("api/{controller}/{action}/{id?}")]
.[FromBody]
should be used for PostReportAction for action method.[HttpPost]
and [HttpGet] attributes should be used in respective ReportViewer interaction methods.You will get compatibility issues with ReportViewer service, which is created from default template. So, following changes required in application StartUp is based on application template type.
You will get the 400 Bad request with service interaction due to the validation of null values with new version of ASP.NET Core. To avoid this issue, you have to remove the [ApiController]
attribute from Report API service. Otherwise, you have to set SuppressModelStateInvalidFilter
to true
in ConfigureApiBehaviorOptions
to avoid this bad request. API behavior options need to updated in ConfigureServices
method in Startup.cs
of server project.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.ConfigureApiBehaviorOptions(options =>
{
options.SuppressModelStateInvalidFilter = true;
});
services.AddResponseCompression(opts =>
{
opts.MimeTypes = ResponseCompressionDefaults.MimeTypes.Concat(
new[] { "application/octet-stream" });
});
}
The following option will be required to avoid the bad request. If you are making the file download application with your Blazor application using location.rel='nofollow' href
.
options.SuppressConsumesConstraintForFormFileParameters = true;
options.SuppressInferBindingSourcesForParameters = true;