π ReportViewerController Documentation
π Table of Contents
- Overview & Purpose
- File Structure & Namespace
- Dependencies & Technologies
- API Endpoints Overview
- Key Functional Areas
- View Models & Data Models
- Helper Methods & Utilities
- Error Handling & Return Types
- Third-Party Integrations
- Usage Examples
- UML Class Diagram
- Conclusion
1. Overview & Purpose
ReportViewerController is an ASP.NET Core API controller designed to host and process Bold Reports (.rdl files) on the server side. It handles incoming requests from a Bold Reports Web ReportViewer control, renders reports in-memory, and streams the results back to the client. Key objectives:
- βοΈ Initialize and configure report options.
- ποΈ Utilize in-memory caching for multi-step report rendering.
- π Load report definitions (
.rdl) from thewwwroot/Resourcesfolder. - π Expose HTTP endpoints to handle POST/GET calls from the ReportViewer.
2. File Structure & Namespace
Path:
IrbyImaging/
βββ Controllers/
βββ ReportViewerController.cs
Namespace:
namespace IrbyImaging.UI.Controllers
The controller implements:
ControllerBase(ASP.NET Core MVC base)IReportController(Bold Reports interface)
3. Dependencies & Technologies
| Category | Library / Framework |
|---|---|
| ASP.NET Core MVC | Microsoft.AspNetCore.Mvc |
| CORS | Microsoft.AspNetCore.Cors |
| Hosting Env | Microsoft.AspNetCore.Hosting.IWebHostEnvironment |
| In-Memory Caching | Microsoft.Extensions.Caching.Memory.IMemoryCache |
| Bold Reports Viewer | BoldReports.Web.ReportViewer |
Note: Ensure BoldReports NuGet package is installed and configured in
Startup.cs.
4. API Endpoints Overview
| HTTP Verb | Route | Description | Auth |
|---|---|---|---|
| POST | /api/ReportViewer/PostReportAction |
Process a viewer action (export, navigate, drill-down) | ββ |
| GET | /api/ReportViewer/GetResource |
Retrieve embedded images or external resources in report | Anonymous |
| POST | /api/ReportViewer/PostFormReportAction |
Alternate entry point for form-based report requests | Anonymous |
5. Key Functional Areas
5.1 Constructor & DI
public ReportViewerController(
IMemoryCache memoryCache,
IWebHostEnvironment hostingEnvironment)
{
_cache = memoryCache;
_hostingEnvironment = hostingEnvironment;
}
IMemoryCache: Stores state between client-report viewer interactions.IWebHostEnvironment: Resolves physical path towwwrootfor loading.rdlfiles.
5.2 PostReportAction
[HttpPost]
public object PostReportAction([FromBody] Dictionary<string, object> jsonArray)
{
return ReportHelper.ProcessReport(jsonArray, this, _cache);
}
- Function: Receives JSON payload from the ReportViewer control.
- Flow: Delegates to
ReportHelper.ProcessReportto perform rendering.
5.3 GetResource
[ActionName("GetResource")]
[AcceptVerbs("GET")]
[AllowAnonymous]
public object GetResource(ReportResource resource)
{
return ReportHelper.GetResource(resource, this, _cache);
}
- Function: Streams images & other report resources.
- Input:
ReportResourceobject (contains ID, MimeType). - Output: Binary content for images in the report.
5.4 PostFormReportAction
[HttpPost]
[AllowAnonymous]
public object PostFormReportAction()
{
return ReportHelper.ProcessReport(null, this, _cache);
}
- Alternate Entry: Used by some BoldReports form-based scenarios without JSON body.
6. View Models & Data Models
ReportViewerOptions(from Bold Reports)- Holds metadata:
ReportModel,DataSources,Parameters, etc.
- Holds metadata:
ReportResource(from Bold Reports)- Contains resource IDs, content type for
GetResource.
- Contains resource IDs, content type for
No custom view models are defined in this controller; all models are provided by BoldReports.
7. Helper Methods & Utilities
7.1 OnInitReportOptions
[NonAction]
public void OnInitReportOptions(ReportViewerOptions reportOption)
{
string basePath = _hostingEnvironment.WebRootPath;
using var fs = new FileStream(
Path.Combine(basePath, "Resources", reportOption.ReportModel.ReportPath),
FileMode.Open, FileAccess.Read);
var ms = new MemoryStream();
fs.CopyTo(ms);
ms.Position = 0;
reportOption.ReportModel.Stream = ms;
reportOption.ReportModel.EmbedImageData = true;
}
- Purpose:
Initializes the in-memory stream for the.rdlreport. - Key Steps:
- Builds the file path (
wwwroot/Resources/{ReportPath}) - Reads into a
MemoryStream - Sets
EmbedImageData = truefor inline images.
- Builds the file path (
7.2 OnReportLoaded
[NonAction]
public void OnReportLoaded(ReportViewerOptions reportOption)
{
// Placeholder: Post-load adjustments can be added here.
}
- Purpose: Hook for any post-load logic (parameters, data modifications).
8. Error Handling & Return Types
- Return Type:
object- Delegates handling to BoldReportsβ
ReportHelper.
- Delegates handling to BoldReportsβ
- Error Propagation:
- Uncaught exceptions bubble up as 500 responses.
- Recommendation:
- Wrap
ProcessReportcalls in try/catch to log errors. - Return structured error responses:
{ "error": { "message": "...", "details": "..." } }
- Wrap
9. Third-Party Integrations
| Integration | Purpose |
|---|---|
| BoldReports.Web | Report Viewer UI & rendering engine |
| In-Memory Cache | Preserve state between multi-step rendering |
| ASP.NET CORS | Enable cross-origin calls from client apps |
10. Usage Examples
10.1 Client-Side JavaScript (Fetch API)
async function renderReport(requestPayload) {
const response = await fetch('/api/ReportViewer/PostReportAction', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(requestPayload)
});
const result = await response.json();
// Pass the result to BoldReports client-side ReportViewer
reportViewer.serviceUrl = '/api/ReportViewer';
reportViewer.render(result);
}
10.2 Startup Configuration
public void ConfigureServices(IServiceCollection services)
{
services.AddMemoryCache();
services.AddControllers();
services.AddCors(options =>
{
options.AddPolicy("AllowAllOrigins",
builder => builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader());
});
// BoldReports configuration...
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
app.UseStaticFiles(); // serve wwwroot
app.UseCors("AllowAllOrigins");
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => endpoints.MapControllers());
}
11. UML Class Diagram
classDiagram
class ReportViewerController {
- IMemoryCache _cache
- IWebHostEnvironment _hostingEnvironment
+ ReportViewerController(IMemoryCache, IWebHostEnvironment)
+ object PostReportAction(Dictionary<string, object>)
+ void OnInitReportOptions(ReportViewerOptions)
+ void OnReportLoaded(ReportViewerOptions)
+ object GetResource(ReportResource)
+ object PostFormReportAction()
}
ReportViewerController ..|> ControllerBase
ReportViewerController ..|> IReportController
ReportViewerController --> ReportHelper : uses
12. Conclusion
The ReportViewerController is a focused, lightweight API controller that bridges BoldReports serverβside processing with an ASP.NET Core web application. By:
- Leveraging in-memory caching for multi-phase rendering,
- Loading
.rdlreport definitions from the wwwroot folder, - Exposing simple HTTP endpoints (
POST/GET) for the ReportViewer control,
you can seamlessly integrate powerful reporting capabilities into your web solution. Remember to configure services (MemoryCache, CORS, BoldReports) in Startup.cs and to place your .rdl files in wwwroot/Resources. Happy reporting! π
Author: IrbyImaging Team
Last Updated: 2024-06-XX