How to use FastReport.Net with ASP.NET Web API
The Web API allows you to quickly and easily create HTTP services. Unlike regular ASP.Net MVC projects, the Web API does not work with views. A controller of a special type is used, the methods of which return model objects.
The task of such a controller is to transmit data, not representations.
Let's look at how to create a simple web service that provides FastReport reports.
1) First of all, we will create two reports, which we will display.
The simple list report template looks like this:
Note that the report header has the [Parameter] parameter. You need to add a report parameter with that name. Data for this report can be taken from the Employee table of the demonstration database nwind.xml, which can be found here: C: \ Program Files (x86) \ FastReports \ FastReport.Net \ Demos \ Reports.
The second report template will not contain data. You can take the ready-made template Barcodes.frx from the folder C: \ Program Files (x86) \ FastReports \ FastReport.Net \ Demos \ Reports.
As mentioned above, we will use two reports and one database in our project. Add them to the folder App_Data. Make a right click on this folder in the solution's browser. Choose Add-> Existing item. Thus we add three files: Barcode.frx, Simple List.frx, nwind.xml. Alternatively, you can simply drag these files to the App_Data folder with the mouse.
2) Create the ASP.NET application:
Click OK and go to the project type selection:
Select the Empty template. At the bottom mark MVC and Web API option. If you select a Web API template, you will receive a project filled with demo data.
3) Add a link to the FastReport.dll library in the Reference.
4) Now, you need to add a data model. To do this, in the solution browser, select the Model folder and make a right click. In the context menu, select Add-> Class:
Give the class name - Reports.cs. The default class type is Class. Click Add.
In the created class, add two variables with the get and set methods:
5) Now add the controller to the project. Do the right click on the Controllers folder. From the context menu, select Add-> Controller.
Select the controller template - Web API2 Controller - Empty:
Call it ReportsController:
Let's proceed to encoding the logic in the controller. The task is to provide a download in the browser or display the report in one of the export formats: PDF, HTML, png.
As you can see, we added another class to the controller. The ReportQuery class defines the HTTP request parameters. This is format, parameter and inline. The first determines the format of the report export, the second one - the parameter value in the report, the third one - whether the report is opened directly in the browser.
In the ReportsController class, we created an array of reports and two methods. The names and report identifiers are defined in the array. The first method GetAllReports () returns a list of available reports. In our case, two reports. The second method GetReportById (int id, [FromUri] ReportQuery query) returns a report on the identifier. From the query attribute, we can get the parameters format, inline, and parameter. They determine: the export format of the report, whether the report will be opened directly in the browser, the value of the parameter to be sent to the report.
6) Add a web page to the report. With it, we will send requests to the server with the necessary parameters. To do this, right click on the project name. Choose Add-> HTML Page:
Set the name – Index:
Add the following code to the page:
As can be understood from the titles, we can:
1. Get a list of reports;
2. Get the first report. Based on our code in the controller, if we do not explicitly pass the format of the format, the report will be displayed in png format;
3. Receive a second report;
4. Get the first report in PDF format;
5. Get the second report in HTML format;
6. Get the first report in PDF format and display it in the browser;
7. Get the second report in HTML format and display it in the browser;
8. Get the first report in PDF format and display it in the browser, and also transfer it to the report.
9. Get the second report in HTML format and display it in the browser, and send it to the report.
7) Open the file WebApiConfig.cs from the folder App_Start. Add one more MapHttpRoute for the Index page:
In the same folder, the RouteConfig.cs file is located. It can be deleted.
8) The last step. Open the file Global.asax. Delete the line:
Now routing will be carried out only through WebApiConfig.
9) Run the application. In the browser we see a list of commands:
- The first link opens a list of reports in the form of an XML document:
- The second and third links will result in downloading the first and second reports in the png format;
- The fourth link leads to the download of the first report in PDF format;
- The fifth link results in the download of the second report in HTML format;
- The sixth link opens the first PDF report directly in the browser:
- The seventh link opens the second report in HTML format directly in the browser:
- The eighth link opens the first report in PDF format in the browser and transmits the REPORT parameter:
- The ninth link opens the first report in HTML format in the browser and transmits the REPORT parameter;
That's all. Working with FastReport in WebAPI is not more difficult than in the common ASP Net MVC project.