Microsoft managed to successfully combine Angular and ASP. Net Core MVC. Therefore, it is relatively easy for us to display reports in a one-page application using the implementation for ASP .Net Core MVC.
You will also need to install .Net Core SDK 2.0 or fresher version. However, this is not necessary if you have Microsoft Visual Studio 2017 installed.
How to create an application?
There are two ways - to create a new project in Visual Studio or to run the command from the command line.
For the first option, you will have to install the Angular application template in the Visual Studio extensions.
And the second is much simpler. Create a folder for your application. In the Windows command line, go to the created folder using the cd command and execute the command:
dotnet new angular -o AngularFRCore
As you understand AngularFRCore is the name of your future project.
After creating the application, you need to install the typescript libraries. We will do this using the Node.js npm platform library installer.
I hope you have not yet closed the console, where we executed the command to create the application?
Run another command while in the application directory:
npm install -g typescript
Now, open the project. Yes, there is no solution file, it will be created when you first start the project.
The goal of our demo application is to show how to use FastReport.Core in a one-page application. Let's add the FastReport library to our project. Open the NuGet package manager. In the upper right corner, you can select the source of the packets. We are interested in a local source, but first you need to configure it. To do this, click on the gear icon in the upper right corner. And we set the path to the local folder with packages:
By default, this folder is: C: \ Program Files (x86) \ FastReports \ FastReport.Net \ Nugets.
By installing FastReport.Net you get ready-made packages in the above pack. Let's return to the package manager:
We have two packages available. Install them.
To use FastReport in an application, you need to add one line of code to the Startup.cs file:
If you glance at the project tree, you can see the Controllers and Models catalogs that are familiar to us from MVC applications. Looking ahead, I will say yes, we can use the almost unchanged MVC .Net Core application as a backend.
There is already one in the Controllers folder - SampleDataController.
Feel free to clear the class contents - we will create our own method:
If you are already familiar with FastReport.Core, then there is nothing new in this method. We created the web report object, loaded the report template into it, created and registered the data source and passed the report to the view. This method has a parameter - the name of the report that we use to load the desired report template.
FastReport.Net set includes a folder with demo reports:
C:\Program Files (x86)\FastReports\FastReport.Net\Demos\Reports
We will take several templates and a nwind.xml database from it. But first, create the App_Data folder in the project root. Now drag files into it from the above folder:
Barcode.frx, Master-Detail.frx, Matrix.frx, nwind.xml.
The next step is to create a view for this method.
There is no Views folder in our project. Create it in the project root. Inside this folder, add another one named SampleData. And, finally, inside this folder we create a new view - ShowReport.chtml with the following content:
In asynchronous mode, we are waiting for the generation of the HTML version of the report.
So, we have prepared a backend, which came with Angular. Let's look at the ClientApp folder. This is a one-page application. We are interested in the src -> app directory. Here are the main files responsible for displaying content - page templates and their type script handlers. The homepage template is app.component.html. We will edit it exactly:
First we display three radio buttons for different reports. Each button is subscribed to the Click event by which we set the value of the report variable. The function takes the name of the report as the parameter. In this way we organize the selection of the desired report template. A fairly primitive implementation, but for the purposes of demonstrating a report, it is no longer necessary.
Below, we show a button called ShowReport, which is also subscribed to the click event with the Clicked () handler. Pay attention on the div with the condition * ngIf = ”show”. If the show variable is true, then the code in the internal tags will be executed. In our case, the frame will be displayed. This is done in order not to display an empty frame when the page is initially loaded. Further, when we select the report and click the Show Report button, the frame with the report will be displayed.
In the frame attributes we set the source from the variable url. Interestingly, we “normalize” this variable with the help of the safeUrl function, applying it through the pipe. This function will check the url for security and valid characters and bring it to the correct form. We will look at it in more details later.
And now, look at the app.component.ts component script, which is responsible for processing the template discussed above:
Here are the show and url variables that we saw in the page template. As well as the variable report, which contains the name of the selected report. The Clicked () function sets the show variable to true and sets the path to the report in the url variable.
And now we will create the safeUrl function, which we used in the pipe with the variable url. In the app directory file safeUrl.pipe.ts:
To use this function in the page template, add the declaration of this pipe module in app.module.ts:
This completes the work on the demo application. Run the application:
First you need to select one of the reports and click the button:
Now select another report and click the button:
Thus, you have seen that using the FastReport.Core report generator in the Angular one-page application is not much more difficult than in the usual ASP .Net Core application.