Roundup #9: Razor Email Templates, AWS Lamba .NET Core 2.1, Contract First API Design with OpenAPI V3

Here are the things that caught my eye this week.  I’d love to hear what you found most interesting this week.  Let me know in the comments or on Twitter.

Walkthrough: Creating an HTML Email Template with Razor and Razor Class Libraries and Rendering it from a .NET Standard Class Library

HTML emails are the absolute worst.  Inlined styles.  Nested tables.  Different apps and platforms render the markup differently.  It’s an absolute train wreck.  However, sending plain text emails isn’t a great way to impress your users and often makes emails look fake or like spam.  So fine, HTML emails.  Let’s do it.

Link: https://scottsauber.com/2018/07/07/walkthrough-creating-an-html-email-template-with-razor-and-razor-class-libraries-and-rendering-it-from-a-net-standard-class-library/

 

AWS Lambda Supports .NET Core 2.1

You can now develop your AWS Lambda function code in C# using the .NET Core 2.1 runtime which will soon be the Long Term Support (LTS) version of .NET Core.

Link: https://aws.amazon.com/about-aws/whats-new/2018/06/lambda-supports-dotnetcore-twopointone/

 

Contract First API Design with OpenAPI V3 – Darrel Miller

The Swagger specification is all grown up with it’s new enterprisey name, OpenAPI and there’s a whole set of new features in V3, like links and callbacks. This session will explore what’s new in OpenAPI V3 while designing and building an HTTP API.

Most Swagger/OpenAPI users started by generating their OpenAPI from an API implementation. As developers gain experience, many start to change their approach to start with an OpenAPI description and build an implementation from that. We will compare the approaches and talk about the advantages of Contract First with OpenAPI.

Link: https://www.youtube.com/watch?v=GtTiIY51kK0

 

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Using Razor in a Console Application (outside of ASP.NET Core MVC)

Razor in a Console ApplicationIn addition up to my previous post, Using ASP.NET Core Razor Views from a Class Library,  I wanted to also dig into how you can use Razor in a Console Application.  Basically from outside of ASP.NET Core MVC.

The most likely situation for this is wanting to generate Emails using Razor that are being sent from a service or console application.  The other might be from another web framework/middleware inside of ASP.NET Core.

To demo how to use Razor outside of ASP.NET CORE MVC, I’m going to use Razor in a Console Application.  Specifically I’m using netcoreapp2.1 for this demo but you can use anything netstandard2.0.

If you’re looking at rendering Razor views from ASP.NET Core MVC for the purpose of generating email content, I recommend checking out Scott Sauber’s blog post: Walkthrough: Creating an HTML Email Template with Razor and Razor Class Libraries and Rendering it from a .NET Standard Class Library.

RazorViewStringRenderer

The heart of the code is actually from RazorViewToStringRenderer.cs in the aspnet/Entropy repo on GitHub.  This repo is for:

A chaotic experimental playground for new features and ideas – check here for small and simple samples for individual features.

NuGet Packages

In our Console App, you’ll need the following packages:

<PackageReference Include=”Microsoft.AspNetCore.Hosting” Version=”2.1.1″ />
<PackageReference Include=”Microsoft.Extensions.FileProviders.Physical” Version=”2.1.1″ />
<PackageReference Include=”Microsoft.Extensions.PlatformAbstractions” Version=”1.1.0″ />

Instantiating

In order to generate a new instance of the RazorViewToStringRenderer class, the simpliest way I’ve found is to create a ServiceCollection and AddMvc() along with a few others.

If you plan on taking this code, you might want to only generate a single instance and reuse it through your application as generating it is fairly costly.

Emails

As mentioned with using Razor for emails, I highly recommend checking out Scott Sauber’s blog post: Walkthrough: Creating an HTML Email Template with Razor and Razor Class Libraries and Rendering it from a .NET Standard Class Library.  It has everything you need to get setup using Razor to generate emails when using ASP.NET Core MVC.

Source

All of the source code for this sample is up in my AspNetCore.Razor.ClassLibrary.Demo repo on GitHub.

If you have any comments or suggestions, please let me know in the comments or on Twitter.

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.

Using ASP.NET Core Razor Views from a Class Library

Razor ViewsI ran into a situation where I wanted to use a Razor views and layouts that were in a .NET Standard 2.0 class library from my ASP.NET Core MVC Project.  There were two issues that I ran into that I will cover in this post.  First is actually getting the MVC project to recognize the views in the class library.  Second was having Rider or Resharper in Visual Studio, understand where the views were located.

Microsoft.NET.Sdk.Razor

In .NET Core 2.1 SDK (or presumably later) there is now a Microsoft.NET.Sdk.Razor MSBuild SDK.

  • Standardizes the experience around building, packaging, and publishing projects containing Razor files for ASP.NET Core MVC-based projects.
  • Includes a set of predefined targets, properties, and items that allow customizing the compilation of Razor files.

In my .NET Standard class library, this means modifying the SDK property to reference Microsoft.NET.Sdk.Razor instead of Microsoft.NET.Sdk

Now if I reference this project from my MVC project, I can use any views located in that class library.

Here’s an outline of my sample solution that shows the project structure.

Now in my DemoControler.cs, I’m using the model and view from the ClassLibrary project.

JetBrains Rider / Resharper

Although this works and the views are located and process correctly at runtime, Visual Studio + ReShaper or JetBrains Rider will complain about now being able to locate the view.

Thankfully, Maarten Balliauw helped me out and provided the solution.

First, you need to add a PackageReference to the NuGet Package JetBrains.Annotations in our MVC project.

Then we can use the AspMvcViewLocationFormat attribute to tell Reshaper/Rider where our views are located.  You can put this attribute anywhere.  I just put in my Startup.cs

At this point ReSharper/Rider will understand where the views are and no longer provide any errors.

Source

All of the source code for this sample is up in my AspNetCore.Razor.ClassLibrary.Demo repo on GitHub.

If you have any comments or suggestions, please let me know in the comments or on Twitter.

Enjoy this post? Subscribe!

Subscribe to our weekly Newsletter and stay tuned.