expressappframework-404176-backend-web-api-service-obtain-a-report-from-a-web-api-controller-endpoint.md
This topic demonstrates how to obtain a report through HTTP requests to the DevExpress Web API Service.
The application must use the following technologies to enable such API requests:
DevExpress ReportingCreates report definitions.XPO | EF CoreThese ORM tools enable storage for report definitions and bound data.XAF Reports ModuleStores report definitions in a database with the help of specially designed classes: ReportDataV2 (XPO) / ReportDataV2 (EF Core).
HTTP request parameters allow you to filter or sort data before the API Controller generates the final report document.
Note
This option of our Web API Service ships as part of the DevExpress Universal Subscription.
The API described in this article only works if your project contains an MVC Controller that allows you to access reports – ReportController. You can use the following methods to enable this controller in your applications:
If you use the Template Kit to create your Backend Web API project, you can enable the Reports module the the Additional Modules section. For additional information, refer to the following topic: Create a Standalone Web API Application.
Install the DevExpress.ExpressApp.ReportsV2.Blazor NuGet package.
Register the Reports module and required services in Startup.cs. Use the Web API builder or XAF Application builder:
Add an API controller with endpoints to download reports to your existing Blazor application. An example of this controller can be found in our demo application that ships with the XAF installation (%PUBLIC%\Documents\DevExpress Demos 25.2\Components\XAF\MainDemo.NET.EFCore\CS\MainDemo.Blazor.Server\API\Reports\ReportController.cs by default).
The ReportController class includes the following methods:
[HttpGet("DownloadByKey({key})")]
public async Task<object> DownloadByKey(string key,
[FromQuery] ExportTarget fileType = ExportTarget.Pdf,
[FromQuery] string criteria = null) {
//...
}
[HttpGet("DownloadByName({displayName})")]
public async Task<object> DownloadByName(string displayName,
[FromQuery] ExportTarget fileType = ExportTarget.Pdf,
[FromQuery] string criteria = null) {
//...
}
This example demonstrates how to obtain a report as a PDF file. The code in this example sends a request to the Report/DownloadByName endpoint with the displayName parameter.
HttpClient httpClient = new HttpClient();
// Set up client Uri.
httpClient.BaseAddress = new Uri("https://localhost:5001/");
// Argument for the DownloadByName method.
var displayName = "Employee List Report";
// Send request for a report PDF.
var response = await httpClient.GetAsync(
$"/api/Report/DownloadByName({displayName})"
);
// Parse the result from HttpResponseMessage.
var report = await response.Content.ReadAsStringAsync();
You can use the HttpRequestMessage API to configure the request locale:
var customRequest = new HttpRequestMessage(HttpMethod.Get,
$"/api/Report/DownloadByName({displayName})");
customRequest.Headers.Add("Accept-Language", "en-US");
var response = await httpClient.SendAsync(customRequest);
For additional information, refer to the following article: Accept-Language header in HttpRequestHeaders.
To specify a filter condition, use a query parameter named criteria. For example, the following URL instructs the report to only include records where “FirstName” equals “Aaron”:
/api/Report/DownloadByName(ReportName)?criteria=[FirstName] = 'Aaron'
Note
ReportController filters data on the server side.
The report itself can apply additional filter conditions to the data source. The criteria set by the report’s FilterString property take effect on the client side, after the data is loaded.
You can use the sortProperty query parameter. For example, the following URL sorts records by FirstName in descending order:
/api/Report/DownloadByName(ReportName)?sortProperty=[FirstName],Descending
Note
The sort order can be overriden by the report. A report band’s SortFields property takes priority if it conflicts with the specified sort order.
If your report uses Parameters, you can pass their values in the query. For example, the following URL sets parameters FirstName and Position to “Mary” and “Manager”, respectively:
/api/Report/DownloadByKey({key})?FirstName=Mary&Position=Manager
The video below shows how you can use our Web Service API in your applications. The instructions guide you through the following tasks:
The full example solution is available on GitHub:
View Example: How to Create a Web API Service Backend for a .NET MAUI Application
To use Web API Authentication, decorate the ReportController with AuthorizeAttribute.
Note that the ReportController uses a report export service: IReportExportService. This service loads a report and prepares it to be exported to the specified format.
using Microsoft.AspNetCore.Mvc;
using DevExpress.ExpressApp.ReportsV2;
public class ReportController : ControllerBase {
private readonly IReportExportService service;
// ...
}
You can modify the controller’s methods as required and even use IReportExportService to create custom endpoints.
The report export service includes helper methods that manage reports and their data sources. Note the SetupReport method that allows you to specify the following parameters before report export: a filter condition and an array of sort properties.
void SetupReport(XtraReport report, string criteria = null,
SortProperty[] sortProperties = null);
Follow the steps below to add unit tests for code that queries reports from the ReportController.
{SolutionName}.Blazor.Server project.Microsoft.AspNetCore.Mvc.Testing package reference.See Also
Authorize EF Core CRUD Operations and Download Reports in .NET MAUI with OData Web API
Authorize EF Core CRUD Operations and Download Reports in Blazor WebAssembly with OData Web API