David HaydenDavid Hayden
  • Home
  • Blog
  • Contact
  • About

Generate Client for ASP.NET Core Web API using OpenAPI

Recently I talked about some of the new features in .NET 5.0 and preview versions of Visual Studio 16.8 and 16.9 with respect to ASP.NET Core Web API Projects. The first announcement I mentioned was the built-in support for OpenAPI and Swagger UI via Swashbuckle in the new ASP.NET Core 5 Web API Project Template, and the other announcement was a new feature introduced in Visual Studio 2019 that allows you to publish the web API to Azure API Management Services as part of the flow of publishing the ASP.NET Core Web API. Both of these tutorials mention Swashbuckle to generate the OpenAPI Specification Document. If you're generating an OpenAPI Specification Document for your ASP.NET Core Web API, you can use this same document to generate a client to consume your web API, which is what I will demonstrate in this ASP.NET Core Web API tutorial.

Recursive Fibonacci and Memoization in C#

The computer science students I tutor are learning memoization using the classic example of recursive Fibonacci. I remember learning these same topics during my data structures and algorithms courses. I also remember being very surprised at the performance before and after memoization.

Publish ASP.NET Core Web API to Azure API Management Services

In a previous ASP.NET Core Web API Tutorial, I mentioned how the new ASP.NET Core Web API Project Template for .NET 5 Framework includes the addition of Swashbuckle.AspNetCore to generate an OpenAPI Specification Document as well as a Swagger UI to explore and test the web API. This works in tandem with a new feature introduced in Visual Studio 2019 that allows you to publish the web API to Azure API Management Services as part of the flow of publishing the ASP.NET Core Web API. Of course, you have the option to opt-out of publishing to Azure API Management Services, and the ASP.NET Core Web API publish flow is smart enough to detect if you aren't using Swashbuckle to generate an OpenAPI Specification Document as well. In this tutorial, I am mainly showing the new OpenAPI feature that I talked about in much more depth in the previous tutorial mentioned above.

EF Core 5 Many-to-Many Relationships

If you're a Microsoft developer using EF Core, you are probably really excited about the new feature in EF Core 5 that makes creating many-to-many relationships super simple! Many-to-many relationships in EF Core 5 work intuitively now, so if you have installed the .NET 5 SDK or Visual Studio 2019 16.9 preview 1 you can test the new feature pretty quickly from a .NET Core Console Application targeting .NET 5. In this EF Core 5 tutorial, I will walk you through the standard sample application of building a many-to-many relationship between blog posts and tags in a SQLite database. I'll also be using the migrations feature in EF Core, because we'll be able to see from the initial migration that indeed the proper database tables are being created to support many-to-many relationships in EF Core 5. I'll also be using the new top-level programs feature in C# 9 to alleviate some of the boilerplate code and nesting of the code.

public class Post
{
    public int PostId { get; set; }

    [Required]
    [StringLength(50)]
    public string Title { get; set; }

    public ICollection<Tag> Tags { get; set; }
}

public class Tag
{
    public int TagId { get; set; }

    [Required]
    [StringLength(25)]
    public string Name { get; set; }

    public ICollection<Post> Posts { get; set; }
}

ASP.NET Core 5 Model Binding to C# 9 Record Types

ASP.NET Core 5 was just released and it includes a number of new features. Earlier I mentioned the built-in support for OpenAPI and Swagger UI via Swashbuckle in the new ASP.NET Core 5 Web API Project Template. Another new feature is improved model binding to support C# 9 record types.

The C# 9 record type is perfect for use as data transfer objects where immutability is valuable. Using them for view models in ASP.NET Core MVC and for transferring data back and forth from an ASP.NET Core Web API seem like good applications for record types.

[HttpPost]
public async Task<ActionResult<Contact>> Post([FromBody]NewContact newContact)
{
    var contact = _mapper.Map(newContact);

    _repository.Add(contact);
    await _repository.SaveChangesAsync();

    return CreatedAtAction("Get", new { id = contact.Id }, _mapper.Map(contact));
}

OpenAPI and Swagger UI in ASP.NET Core 5 Web API

Microsoft released ASP.NET Core 5. One of the new features mentioned in the ASP.NET Core 5 announcement is the "built-in" support for OpenAPI and Swagger UI in ASP.NET Core 5 Web API's. This isn't so much a new built-in feature as it is a change in the ASP.NET Core 5 Web API Project Template. Microsoft is simply including Swashbuckle as part of the template and configuring it with some default settings for your project.

Top-Level Programs in C# 9

Top-level programs in C# 9 remove the boilerplate and nesting in code, making it so much easier to share working sample C# code on my blog.

using System;

Console.WriteLine("Hello World!");

Guard Clauses in Computer Science

While assisting my computer science students on ways to help debug and improve the clarity and runtime of their code, I explained the concept of guard clauses. Guard clauses are true/false expressions (predicates) found at the top of a method or function that determine whether the function should continue to run. Guard clauses test for preconditions and either immediately return from the method or throw an exception, preventing the remaining body of code from executing.

C# 9 Record Type

The new record type in C# 9 is intended for immutability and has a number of cool features, including positional records, value-based equality, a useful ToString override, and support for the with-expression and init accessor.

record Person(string Name, int Age) { }

class Program
{
    static void Main(string[] args)
    {
        var person = new Person("John Doe", 18);

        Console.WriteLine($"Name: {person.Name}, Age: {person.Age}");
    }
}

// Output
Name: John Doe, Age: 18

C# 9 Init-Only Properties

C# 9 introduces init-only properties that allow you to set properties on a class that can only be set once during object initialization.

This is done using a new init accessor introduced in C# 9 as part of a class declaration.

public class Person
{
    public string Name { get; init; }
    public int Age { get; init; }
}

Orchard Core CMS Shortcodes and Shortcode Delegates

We talked about the new Shortcodes and Shortcode Templates Features in the previous Orchard Core CMS Tutorial. In this tutorial, let's create an Orchard Core CMS shortcode in code using a shortcode delegate: Orchard Core CMS Shortcodes and Shortcode Delegates. You can add these shortcodes as part of your custom Orchard Core CMS Theme or Module.

Orchard Core CMS Shortcodes and Shortcode Delegates

Shortcodes and Shortcode Templates in Orchard Core CMS

Orchard Core CMS supports shortcodes to make adding content to your Orchard Core CMS website easier! As an Orchard Core Developer it is possible to include shortcodes in custom modules and custom themes, but by far the easiest way to add shortcodes is via the Shortcode Templates feature. By enabling Shortcode Templates, one can create shortcodes in the backend with just a little knowledge of HTML and the liquid template language - no complex coding necessary.

I created an Orchard Core CMS tutorial that walks you through creating a Bootstap Alert shortcode using the Shortcode Templates feature. I also show you how to use the new shortcode to easily add a Bootstrap Alert to your website content: Shortcodes and Shortcode Templates in Orchard Core CMS. In future tutorials I will show you how to create shortcodes via a custom IShortcodeProvider to include as part of your custom module or custom theme. As an FYI, here is another tutorial on shortcode templates in Orchard Core CMS.

Shortcode Templates Feature Orchard Core CMS

Create Orchard Core CMS Website

Attended a local developer group meeting and was asked how to create an Orchard Core CMS Website. Turns out this is really easy whether you're an Orchard Core Developer or not. The Orchard Core Code Generation Templates that work with the .NET Core CLI make it really easy to create a website, Orchard Core Module, Orchard Core Theme, and even a modular .NET Core MVC Website. Here is a quick Orchard Core CMS Tutorial that shows you how to create an Orchard Core CMS Website.

Create Orchard Core CMS Website

Orchard Core Blog and Custom Recipes

I promised in an earlier Orchard Core CMS Tutorial that I would mention a feature in Orchard Core that allows developers to expose custom recipes in their Orchard Core CMS Themes and Modules and run them from the admin dashboard. Although a bit bare bones at the time of this tutorial, custom recipes are a very handy way for Orchard Core CMS Developers to add additional functionality and features to their custom themes and modules on the fly. I currently use custom recipes in my themes to add features like blogs, portfolios, product catalogs for e-commerce stores, FAQ's, etc. on demand. I also use custom recipes in my custom Orchard Core CMS Modules to help with special situations or monotonous tasks. Learn more about running custom recipes and setup recipes in Orchard Core CMS from the admin dashboard.

Running Custom Recipes in Orchard Core CMS from Admin Dashboard

Portfolio Widget - Orchard Core Theme Development

Developed a cool portfolio widget for one of my Orchard Core CMS Themes, and it got me thinking just how much I enjoy developing widgets, modules, themes, websites, modular .NET Core Apps, etc. with Orchard Core CMS. I am completely sold on Orchard Core and am thrilled to be an Orchard Core Developer. The liquid template language and the Templates Module are a real joy to use for creating shapes and views. I still enjoy Razor, but man, liquid is fun, and even cooler, I am able to and have developed custom liquid template language filters in Orchard Core to create new features and functionality! I'll be bundling the new portfolio widget as a custom recipe in my theme, which yeah... is a little tedious right now, but has some incredible advantages in terms of extensibility, separation of concerns, etc. So, I love creating those custom recipes, too, because they provide that extra BAM I love to give to my themes!

Orchard Core Development - Portfolio Widget

Taxonomies in Orchard Core CMS

Taxonomies Module was recently added to Orchard Core! Taxonomies allow you to categorize various content items in your Orchard Core CMS Website. I wrote a quick Orchard Core Tutorial that shows how to enable the Taxonomies feature, add a new Taxonomy Term Content Type, create a new Taxonomy, and attach the new Taxonomy Field to a Content Type. The tutorial categorizes movies by genres in a movie library.

Taxonomies in Orchard Core CMS

Recipe Migrations in Orchard Core CMS

I developed a custom Orchard Core CMS Module that creates a Bootstrap 4 Card Widget to test the new Recipe Migrations Feature. The new IRecipeMigrator Interface in Orchard Core can be used in place of IContentDefinitionManager in DataMigration Classes to execute recipe migration files. Having developed numerous setup recipes for custom Orchard Core CMS Themes (Coming Soon, Freelancer, Editorial, and more) as well as recipes that can be run from the Orchard Core Dashboard to add features on the fly, I much prefer using the new Recipe Migrations feature over IContentDefinitionManager when possible and appropriate.

Custom Orchard Core CMS Module Bootstrap 4 Card Widget

Using GraphQL via GraphiQL in Orchard Core CMS

GraphQL and GraphiQL were recently added to Orchard Core. A great way to get started with GraphQL in Orchard Core is to create a website using The Agency Theme and query the various BagParts and their repected Content Types, Parts, and Fields using the GraphQL Explorer (i.e. GraphiQL). In this Orchard Core CMS Tutorial I use GraphQL and GraphiQL to query the portfolio and its projects.

GraphiQL Intellisense in Orchard Core CMS

Develop a Custom Widget in Orchard Core CMS

If you're interested in Orchard Core CMS Development, you will enjoy this tutorial on developing a custom widget for Orchard Core CMS. From scratch, I show how to develop an Alert Widget using the Orchard Core admin and liquid templates. I create the Alert Widget Content Type, add a Text Field and HtmlBody Part, demonstrate how to set field settings for the Predefined List Editor (aka Options Editor), and develop the custom templates using Liquid with the support of the Template Module in Orchard Core CMS. If you're new to Orchard Core CMS, you should learn a few tips and tricks on Orchard Core Development.

Develop Custom Widget for Orchard Core CMS

Orchard Core CMS Theme Development

Let's continue working with The Blog Theme in Orchard Core CMS and add a new zone to the layout template for displaying shapes. We're not going to modify the original theme, however. Instead let's use the Templates Module in Orchard Core CMS to create a new Layout template that overrides the theme layout. If you're interested in becoming an Orchard Core CMS Developer, view the tutorial: Adding a New Zone to The Blog Theme Layout in Orchard Core CMS

Add Zone to The Blog Theme Layout in Orchard Core CMS

Templates in Orchard Core CMS

A new Orchard Core CMS tutorial showing how to override templates in The Blog Theme using the Templates Module. Let's enhance The Blog Theme by allowing authors to select a background image for articles using a Media Field and display the selected background image in a new liquid template that overrides The Blog Theme template. Developers interested in being Orchard CMS Theme Developers or extending the current themes in Orchard Core CMS will find this tutorial useful.

Liquid Templates in Orchard Core CMS

Available for Hire

Looking for a project starting February 2021.

Contact Me

OrchardCore.io

I recently started a new website, OrchardCore.io, dedicated to tutorials and resources on Orchard Core. I added a new section for Orchard Core Developer Notes. The notes are bite-size tips and solutions to common Orchard Core challenges. Recent notes include: GraphQL Permissions in Orchard Core, OpenID Connect and Token Validation, the HTML Sanitizer in Orchard Core CMS, and enabling the new Code Mirror Editor for HTML Field and Text Field.

Visit OrchardCore.io
David Hayden

David Hayden is a freelance Microsoft developer in Sarasota, Florida specializing in Orchard Core CMS web applications and websites. David is also fluent in .NET Core, C#, ASP.NET Core MVC, Razor Pages, SQL Server, Web API, and Azure Cloud Services. David also mentors college students in computer science using C, C++, Java, and Python.

David is also a NASM certified Personal Trainer, Nutrition Coach, Corrective Exercise Specialiist, and Weight Loss Specialist. In addition, David is a Precision Nutrition Level 1 Coach and Optimize Level 1 Coach.

Discover OrchardCore.io

I recently started a new website, called OrchardCore.io, dedicated to Orchard Core tutorials and resources.

  • Orchard Core MediaField
  • Dashboard Theme for Orchard Core
  • Custom User Settings in Orchard Core
  • Custom Settings in Orchard Core
  • Orchard Core UserOptions as Security Best Practice
  • Orchard Core from Scratch
Navigation
  • Home
  • Blog
  • Contact
  • About
  • Site Map
Copyright © 2021 David Hayden.