paint-brush
6 Ways to Generate HTML Documents With C# .Netby@vitaliihoncharuk
33,814 reads
33,814 reads

6 Ways to Generate HTML Documents With C# .Net

by Vitalii HoncharukMarch 2nd, 2021
Read on Terminal Reader
Read this story w/o Javascript

Too Long; Didn't Read

6 Ways to Generate HTML Documents With C#.Net are some options how to generate HTML documents. RazorEngine requires lots of dependencies and obsolete libraries. Handlebars.Net is a really easy and fast way to do documents generation because it doesn't use any scripting engine to run a Javascript library; it compiles Handlebars templates directly to IL bytecode. The simplest and fastest way for simple files generation would be to go with simple templating which will not bring new dependencies into the project.
featured image - 6 Ways to Generate HTML Documents With C# .Net
Vitalii Honcharuk HackerNoon profile picture

Reason for the article:

  • Static HTML documents generation
  • Email body generation

Parameters to compare:

  • Straightforward usage
  • Minimum complexity
  • Minimum dependencies

I went through a ton of articles and questions on stack overflow, stack exchange, here are some options how to generate HTML documents:

1. RazorEngine

While checking the RazorEngine approach I was not able to run it on .Net Core. It requires lots of dependencies and obsolete libraries.

Knowing that everybody would avoid obsolete technologies and .Net framework libs I think going with this generator is not the right way.

2. RazorLight

After some research, I was able to run the Razor engine using this library on “netstandard” library, please see the following code below.

Pros:

  • Powerful generation engine

Cons:

  • It brings lots of libraries in the dependencies
  • The library is not official
  • A latest valid working version is 2.0.0-rc.3
  • May require DevOps effort to make it work on the environments
  • Requires next property inside PropertyGroup for the app which uses this library
<PreserveCompilationContext>true</PreserveCompilationContext>

3. HtmlContentBuilder

HtmlContentBuilder is more suitable for dynamic HTML creation and requires lots of preparation to build actual HTML output. I do not see the benefits of using this library for current goals.

4. Web App to Call Locally and Render HTML on Serverside

This idea died in my head at the very beginning, since to achieve this we needed to:

  • Create a separate resource using aspnet core, asp.net framework or other
  • Maintain this resource
  • Maintain dispatcher/client for the resource
  • Make calls to this service endpoints locally from any place you need to generate an HTML document
  • Send data required for rendering in the body of each request or implement data retrieving before generating an HTML

5. Simple Templating

Here is the code example with the simplest and atomic approach implemented in .net core console application:

Key points:

  • No dependencies and external apps required
  • It may be implemented in any place and will not complicate the system
  • Nothing new for C# developer
  • Each HTML builder is atomic and knows what to build and where to place data
  • It may have the functionality to get the template from a file
  • It may call some additional builders to get HTML from there (f.e. for header and footer)
  • It may have the functionality to get some common HTML or CSS from another file

6. Handlebars.Net

After all the previous disappointing findings I started locking in using JS frameworks to do the generation of documents, that is how I found Handlebars.js but written fully in .NET.

So Handlebars.Net is a really easy and fast way to do documents generation because it doesn't use any scripting engine to run a Javascript library; it compiles Handlebars templates directly to IL bytecode. It also mimics the JS library's API as closely as possible.

Since Handlebars.js has a big community and a long story you will be able to find answers to your questions to build a valid HTML.

The link to their Github repo is here.

Conclusion

The simplest and fastest way for simple files generation would be to go with simple templating which will not bring new dependencies into the project.

On the other hand, people who know Razor may be easier to go with a powerful Razor engine via RazorLight, but it may require DevOps effort to make sure dependent libraries are in place in the environments. And I think the easiest and most extendable way would be Handlebars.Net which is straightforward in usage.

In addition

All these findings can be applied not only to generate HTML files, but for XML files and any plain text documents.

Also, this article will be useful for those who are looking for a way to generate PDF documents since data sources for the generation of PDF files may be HTML string. A good topic to explore next would be: "How to convert HTML to PDF."

Lead image courtesy of Nathan da Silva on Unsplash.