Adliance.AspNetCore.Buddy.Template.Razor 8.0.0.2

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
dotnet add package Adliance.AspNetCore.Buddy.Template.Razor --version 8.0.0.2
NuGet\Install-Package Adliance.AspNetCore.Buddy.Template.Razor -Version 8.0.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Adliance.AspNetCore.Buddy.Template.Razor" Version="8.0.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Adliance.AspNetCore.Buddy.Template.Razor --version 8.0.0.2
#r "nuget: Adliance.AspNetCore.Buddy.Template.Razor, 8.0.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Adliance.AspNetCore.Buddy.Template.Razor as a Cake Addin
#addin nuget:?package=Adliance.AspNetCore.Buddy.Template.Razor&version=8.0.0.2

// Install Adliance.AspNetCore.Buddy.Template.Razor as a Cake Tool
#tool nuget:?package=Adliance.AspNetCore.Buddy.Template.Razor&version=8.0.0.2

Adliance.AspNetCore.Buddy.Template.Razor

This razor templating library offers several rendering options of templates.

  • Render a template to HTML (what Razor usually does)
  • Render a template and send it as email (HTML and text version).
  • Generate a PDF with header and footer.

Setup library in an ASP.NET project

When adding the Razor templating mechanism, it's necessary to add the MVC services for View support to the ASP.NET project with the AddControllersWithViews extension method.

The IBuddyServiceCollection of the Adliance.AspNetCore.Buddy.Abstractions package offers the AddBuddy method, which several extensions to add the templating and PDF services.

  • AddRazorTemplater()
  • AddPdf(configuration)
  • AddRazorPdfV1Renderer()
  • AddRazorPdfV2Renderer()
public void ConfigureServices(IServiceCollection services)
{
  //...
  services.AddBuddy()
    .AddRazorTemplater()
    .AddPdf(Configuration.GetSection("Pdf"))    
    .AddRazorPdfV2Renderer();
  //Don't forget to add MVC services to your project.
  services.AddControllersWithViews();
  //...
 }

Configuration (appsettings.json)

Add a section in the configuration of your project and point to the used PDF service endpoint.

{
  "Pdf": {
    "ServerUrl": "https://url-to-pdf.service"
  }
}

Health check

The library also offers an extension to the IHealthChecksBuilder to add a health check to the used PDF service.

services.AddHealthChecks()
  .AddPdfCheck();

Usage of library

There are following three possibilities to use the library.

Render HTML

In the Views directory in your project, you have to add a new directory containing the template you want to render.

The structure would look like:

  • Views
    • DirectoryName
      • {TemplateName}.cshtml

The usage looks like:

ITemplate _templater = 
await _templater.Render("DirectoryName", "TemplateName", viewModel);

Provide the name of the directory and the template. Usually a template has a view model, which is the third parameter.

Render and send an email

To use the full functionality of this option consider using another buddy package Adliance.AspNetCore.Buddy.Email.Mailjet to send the rendered templates or implement the email logic by yourself.

In the Views directory of your project, you have to add a new EmailTemplates directory. Each "email" consists of three templates, one for the subject, two for the content (HTML and text). The structure would look like:

  • Views
    • EmailTemplates
      • {TemplateBaseName}.Subject.cshtml
      • {TemplateBaseName}.Html.cshtml
      • {TemplateBaseName}.Text.cshtml

Use the email renderer by providing the recipient as first parameter, the name of the base template as second and the viewModel as last.

await _emailRenderer.RenderAndSend("recipient@adliance.net", "TemplateBaseName", viewModel);

Render and send a SMS

To use the full functionality of this option consider using another buddy package Adliance.AspNetCore.Buddy.Sms.Twilio to send the rendered templates or implement the SMS logic by yourself.

In the Views directory of your project, you have to add a new SmsTemplates directory.

  • Views
    • SmsTemplates
      • {TemplateBaseName}.cshtml

Use the SMS renderer by providing the recipient as first parameter, the name of the base template as second and the viewModel as last.

await _smsRenderer.RenderAndSendAsync("+431234567890", "TemplateBaseName", viewModel);

Or you use the other RenderAndSendAsync overload where you can specify the template directory name yourself.

Render a PDF

This option uses the Adliance.AspNetCore.Buddy.Pdf package to generate PDFs.

To render a PDF template, you need to provide four cshtml files in a PdfTemplates directory in the Views directory of your project.

The default template structure is:

  • Views
    • PdfTemplates
      • {TemplateBaseName}.cshtml
      • {TemplateBaseName}.Filename.cshtml
      • {TemplateBaseName}.Header.cshtml
      • {TemplateBaseName}.Footer.cshtml

This code sample shows the usage of the PDF renderer in a controller action, which is directly returning the resulting PDF as file content.

PdfRendererResult result = await _pdfRenderer.Render("TemplateBaseName", viewModel);
return new FileContentResult(result.Bytes, MediaTypeNames.Application.Pdf)
{
	FileDownloadName = result.Filename
};

PDF library documentation

See more detailed information in the PDF library

Razor Tag Helpers

Add the Buddy Tag Helper to your _ViewImports.cshtml file.

@using System
@using System.Collections.Generic

@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@addTagHelper *, Adliance.AspNetCore.Buddy.Template.Razor
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
8.0.0.2 195 4/11/2024
8.0.0.1 668 3/15/2024
6.0.16 7,905 11/16/2022
6.0.14 314 11/16/2022
6.0.11 2,350 7/25/2022
6.0.10 860 7/25/2022
6.0.9 409 7/23/2022
6.0.8 605 7/10/2022
6.0.7 401 7/10/2022
6.0.5 534 6/20/2022
6.0.4 419 6/20/2022
6.0.3 583 6/8/2022
6.0.2 412 6/8/2022
6.0.1 405 6/8/2022
5.4.13 509 5/30/2022
5.4.10 3,653 10/28/2021
5.4.7 1,388 9/7/2021
5.4.5 734 8/19/2021
5.4.4 335 8/19/2021
5.4.2 2,624 6/9/2021
5.4.1 408 6/5/2021
5.3.2 1,612 4/2/2021
5.3.1 373 4/2/2021
5.2.2 433 3/3/2021
5.2.1 733 1/14/2021
5.1.2 1,245 12/1/2020
5.1.1 381 12/1/2020
5.0.7 460 11/19/2020
5.0.6 424 11/19/2020
5.0.3 435 11/19/2020
5.0.2 462 11/19/2020
4.0.10 1,393 9/21/2020
4.0.9 736 9/20/2020
4.0.8 481 9/20/2020
4.0.7 484 9/20/2020
4.0.6 535 9/15/2020
4.0.5 510 9/13/2020
4.0.3 511 9/13/2020
4.0.2 493 9/13/2020
4.0.1 502 9/13/2020