Set up C# project
Note
This section assumes, that you have a working .NET 8 setup. If that is not the case, please follow the official Microsoft guide to install .NET on your machine.
Let's start with a fresh ASP.NET project by executing dotnet new webapi
dotnet new webapi
in a new directory. The starter project has a single endpoint defined in Program.cs
Program.cs
. We can try that endpoint out in the swagger UI. Start up the service (dotnet run
dotnet run
) and follow the link printed in the terminal. If the page is empty, navigate to /swagger
/swagger
. In the swagger UI, expand the /weatherforecast
/weatherforecast
endpoint, press "Try it out", then "Execute". This will send an HTTP request to the running ASP.NET service and return made up weather data.
We will define a new endpoint to compile our Oicana template to a PDF and return the PDF file to the user.
- Create a new directory in the .NET project called
templates
templates
and copyexample-0.1.0.zip
example-0.1.0.zip
into that directory. - Add the
Oicana
Oicana
NuGet package as a dependency withdotnet add package Oicana --prerelease
dotnet add package Oicana --prerelease
. -
Read the template file and prepare it for compilation at the beginning of
Program.cs
Program.cs
: -
Replace the generated
/weatherforecast
/weatherforecast
endpoint with the following:This code defines a new POST endpoint at
/compile
/compile
. For every request, it compiles the template to PDF with two empty input lists and returns the file.
After restarting the service and refreshing the swagger UI, you should see the new endpoint. Open up the endpoint description and click "Try it out" and "Execute" to send a request to the server. You should see a successful response with a download button for the PDF file.
The PDF generation should not take longer than a couple of milliseconds. You can look at the request duration in the network tab of your browser's debugging tools for an estimation. The first request to an APS.NET service can be significantly slower than later ones, because ASP.NET does some preparation during the first request.
For a better measurement of the compilation speed on your machine, you can use a Stopwatch
Stopwatch
in the endpoint code.
Next up: add dynamic inputs to the Oicana template.