Skip to main content
A newer version of this page is available. .
All docs
V22.1

Open a Report in ASP.NET Core Application

  • 4 minutes to read

This topic describes several ways to display a report in a Document Viewer Control. It also addresses common issues that may occur when the DevExpress Document Viewer opens a report.

Specify a Report

Once incorporated into a web page, the DevExpress Document Viewer control can automatically load a report at “startup.” The Bind method allows you to bind the Document Viewer to a View Model or report. You can create a new report instance and pass it as the parameter or specify a report by name.

@model DevExpress.XtraReports.Web.WebDocumentViewer.WebDocumentViewerModel;
//...
@{
    var viewerRender = Html.DevExpress().WebDocumentViewer("DocumentViewer")
        .Height("1000px")
        .Bind(Model);
    @viewerRender.RenderHtml()
}

Bind to a View Model

Your controller must use the WebDocumentViewerClientSideModelGenerator object to create a View Model. You can call the GetModel method overload to create a report model and pass it to the view.

This strategy allows you to generate reports asynchronously. You can call the UseAsyncEngine method (at app startup) to activate asynchronous mode, and use the GetModelAsync method to proceed:

using DevExpress.AspNetCore.Reporting.WebDocumentViewer;
using DevExpress.XtraReports.Web.WebDocumentViewer;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
// ...
    public class HomeController : Controller {        
        public async Task<IActionResult> Index([FromQuery] string reportName = "MainReport") {
            var modelGenerator = new WebDocumentViewerClientSideModelGenerator(HttpContext.RequestServices);
            var model = await modelGenerator.GetModelAsync(reportName, WebDocumentViewerController.DefaultUri);
            return View(model);
        }
    }

Pass a Report Instance

You can instantiate a report and pass it to the Bind method as necessary. This approach consumes more memory because a new report instance is generated when the page reloads.

You can pass the CachedReportSourceWeb instance to the Bind method to optimize memory usage. As its name implies, our CachedReportSourceWeb object caches a document once generated.

Specify a Report Name

If you specify a report name as the Bind method parameter, the Document Viewer uses report name resolution services (described below) to obtain the report by name.

You can also use client-side APIs and call the OpenReport method of the client Document Viewer object, or handle the Document Viewer client-side event and call the OpenReport method of the event sender object. These methods accept report name as a parameter and use report name resolution services.

Use Report Name Resolution Services

If you pass report name to a method that opens a report, you must implement and register one of the following services (to resolve report names).

IWebDocumentViewerReportResolver
Allows you to parse a report name, create a report instance, and return it to the calling method. If you have a parameterized report, you can specify parameters in the report name passed to the service and use parameters within the report’s constructor.

Note

The IWebDocumentViewerReportResolver service does not support asynchronous mode.

ICachedReportSourceWebResolver
Returns an instance of CachedReportSourceWeb. The ICachedReportSourceWebResolverAsync service allows you to build a document asynchronously.
IReportProvider
This service has lower priority than previously mentioned services (and is not intended for exclusive use by the Document Viewer). The primary advantage of IReportProvider service is that it can be attached to reports created at runtime. IReportProviderAsync uses asynchronous operations.
ReportStorageWebExtension
This service is called when no other report name resolution services are available. It is designed to obtain reports (stored in our REPX format) from external storage (a file or a database). Note that the GetData method returns a serialized report. If you use the GetData method to specify the default parameter value for a loaded report, set the Value property to the parameter value.

Open Subreports

The XRSubreport control defines additional reports (subreports) included in the host report. The control’s ReportSource property specifies the report instance used as a subreport, and the ReportSourceUrl property specifies report name. The ReportSourceUrl property has priority over the ReportSource property.

A subreport is opened automatically alongside the main report. To open a subreport, the Document Viewer passes the XRSubreport.ReportSourceUrl property value to available report name resolution services, except the following:

Consider an application in which both IWebDocumentViewerReportResolver and IReportProvider services are available. When the Document Viewer opens a report with an associated subreport, the IWebDocumentViewerReportResolver service resolves the main report name while the IReportProvider service is called to resolve the subreport name.