Adliance.AspNetCore.Buddy.Template.Razor
8.0.0.2
Prefix Reserved
See the version list below for details.
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
<PackageReference Include="Adliance.AspNetCore.Buddy.Template.Razor" Version="8.0.0.2" />
paket add Adliance.AspNetCore.Buddy.Template.Razor --version 8.0.0.2
#r "nuget: Adliance.AspNetCore.Buddy.Template.Razor, 8.0.0.2"
// 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
- DirectoryName
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
- EmailTemplates
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
- SmsTemplates
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
- PdfTemplates
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 | Versions 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. |
-
net8.0
- Adliance.AspNetCore.Buddy.Abstractions (>= 8.0.0.5)
- Adliance.AspNetCore.Buddy.Pdf (>= 8.0.0.2)
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.3 | 4,009 | 6/24/2024 |
8.0.0.2 | 587 | 4/11/2024 |
8.0.0.1 | 1,800 | 3/15/2024 |
6.0.16 | 8,850 | 11/16/2022 |
6.0.14 | 347 | 11/16/2022 |
6.0.11 | 2,417 | 7/25/2022 |
6.0.10 | 887 | 7/25/2022 |
6.0.9 | 440 | 7/23/2022 |
6.0.8 | 637 | 7/10/2022 |
6.0.7 | 427 | 7/10/2022 |
6.0.5 | 565 | 6/20/2022 |
6.0.4 | 451 | 6/20/2022 |
6.0.3 | 611 | 6/8/2022 |
6.0.2 | 442 | 6/8/2022 |
6.0.1 | 428 | 6/8/2022 |
5.4.13 | 535 | 5/30/2022 |
5.4.10 | 3,684 | 10/28/2021 |
5.4.7 | 1,419 | 9/7/2021 |
5.4.5 | 763 | 8/19/2021 |
5.4.4 | 348 | 8/19/2021 |
5.4.2 | 2,650 | 6/9/2021 |
5.4.1 | 421 | 6/5/2021 |
5.3.2 | 1,641 | 4/2/2021 |
5.3.1 | 399 | 4/2/2021 |
5.2.2 | 462 | 3/3/2021 |
5.2.1 | 763 | 1/14/2021 |
5.1.2 | 1,271 | 12/1/2020 |
5.1.1 | 417 | 12/1/2020 |
5.0.7 | 484 | 11/19/2020 |
5.0.6 | 454 | 11/19/2020 |
5.0.3 | 469 | 11/19/2020 |
5.0.2 | 486 | 11/19/2020 |
4.0.10 | 1,429 | 9/21/2020 |
4.0.9 | 758 | 9/20/2020 |
4.0.8 | 519 | 9/20/2020 |
4.0.7 | 514 | 9/20/2020 |
4.0.6 | 559 | 9/15/2020 |
4.0.5 | 547 | 9/13/2020 |
4.0.3 | 541 | 9/13/2020 |
4.0.2 | 517 | 9/13/2020 |
4.0.1 | 526 | 9/13/2020 |