---
title: "How to build a URL Shortener with C# .NET and Redis"
description: "Learn how to build a link shortener using C#, .NET, and Redis. You'll be using ASP.NET Core to build the URL forwarder and the System.CommandLine libraries to manage the data."
canonical: "https://www.twilio.com/en-us/blog/developers/tutorials/building-blocks/build-a-url-shortener-with-csharp-dotnet-and-redis"
published: "2023-03-21T00:00:00.000Z"
updated: "2023-03-22T01:06:56.573Z"
---

# How to build a URL Shortener with C# .NET and Redis

**This blog post was written for [Twilio](https://www.twilio.com/) and [originally published at the Twilio blog](https://www.twilio.com/blog/build-a-url-shortener-with-csharp-dotnet-and-redis).**

URLs are used to locate resources on the internet, but URLs can be long making them hard to use. Especially when you have to type the URL manually, even more so when having to do so on a mobile phone. Long URLs are also problematic when you have a limited amount of characters such as within an [SMS segment](https://www.twilio.com/blog/2017/03/what-the-heck-is-a-segment.html) or a Tweet.

A solution to this is to use a URL shortener which will create URLs that are short and sweet. When you open the shortened URL, the URL will forward you to the long destination URL. This makes it easier to manually type the URL and *also* save precious characters.

Shortened URLs also obfuscate the real URL so users don't know where they will land on the internet when they click the URL. This could be abused by malicious actors, so make sure your URL shortener is secure! If you're using a free URL shortener, users may be wary because anyone could've created the shortened URL. Using your own branded domain name will establish more trust in the shortened URLs you send.

In this tutorial, you'll learn how to create your own URL shortener using C#, .NET, and Redis.

## Prerequisites 

Here 's what you will need to follow along:

-   [.NET 6 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/6.0) (later versions will work too)
-   A code editor or IDE. I recommend [JetBrains Rider](https://www.jetbrains.com/rider/), [Visual Studio](https://visualstudio.microsoft.com/downloads/), or [VS Code](https://code.visualstudio.com/Download) with [the C# plugin](https://marketplace.visualstudio.com/items?itemName=ms-dotnettools.csharp).
-   [A Redis database](https://redis.io/docs/getting-started/)

You can find [the source code for this tutorial on GitHub](https://github.com/Swimburger/UrlShortener). Use it if you run into any issues, or [submit an issue](https://github.com/Swimburger/UrlShortener/issues), if you run into problems.

Before creating your application, let's take a look at how URL shorteners work and what you'll be building.

## URL Shortener Solution 

<figure><img src="https://twilio-cms-prod.s3.amazonaws.com/documents/URL_Shortener_Diagram.svg" alt="Diagram of a mobile user browsing to a shortened URL (1), which sends an HTTP request to the Forwarder App, the Forwarder App retrieves the destination URL from the Redis Database (2). The Forwarder App responds with a redirect to the destination URL to the mobile user. The other part of the diagram starts from an administrator that is using a command-line interface (CLI) application, the CLI app communicates with the Redis database to manage shortened URLs." width="810" height="145"></figure>

There are two parts to URL shorteners: forwarding the shortened URLs to the destination URL and the management of the shortened URLs. While these two parts *could* live within a single application, you will build separate applications for URL management and for URL forwarding.

The two applications will work together as shown in the diagram above.

An administrator can use the CLI CRUD (**C**reate **R**ead **U**pdate **D**elete) application (1) to manage the shortened URL data in a Redis Database (2).

Let's say a mobile user received an SMS with a shortened URL. When the user clicks on the shortened URL, the web browser sends an HTTP GET request to that URL which is received by the Forwarder App (1). The forwarder app will look at the path of the request and look up the destination URL in the same Redis Database and return it to the Forwarder App (2). The Forwarder App then sends back an HTTP response with status code [307 Temporary Redirect](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307) pointing towards the destination URL (3).

You'll build the Forwarder App using an [ASP.NET Core Minimal API](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis), and you'll also build a command-line interface (CLI) application to provide the CRUD functionality. The CLI application will take advantage of [the open-source System.CommandLine libraries](https://github.com/dotnet/command-line-api). While these libraries are still in preview, they work well and provide common functionality needed to build CLI applications. While you'll build the CRUD functionality in a CLI app, you could build the same app as a website or API using ASP.NET Core, or client app using [WPF](https://learn.microsoft.com/en-us/visualstudio/designers/getting-started-with-wpf?view=vs-2022), [WinForms](https://learn.microsoft.com/en-us/dotnet/desktop/winforms/overview/?view=netdesktop-6.0), [Avalonia](https://avaloniaui.net/), [MAUI](https://learn.microsoft.com/en-us/dotnet/maui/what-is-maui), etc.

The CLI application will store the collection of shortened URLs in a Redis database. The key of each shortened URL will be the unique path that will be used to browse to the URL, and the value will be the destination URL.

I chose Redis for this application because it works well as a key-value database, it is well-supported, and there's a great .NET library to interact with Redis. However, you could use any data store you'd like in your own implementation.

Lastly, since these two applications will interact with the same data store and perform some of the same functionality, you'll create a class library for shared functionality for the CRUD operations and validation.

## Create the Data Layer 

First, open a terminal and run the following commands to create a folder, navigate into it, and create a solution file using the .NET CLI:

```bash
mkdir UrlShortener
cd UrlShortener
dotnet new sln
```

Then, create a new class library project for the data layer, and add the library to the solution:

```bash
dotnet new classlib -o UrlShortener.Data
dotnet sln add UrlShortener.Data
```

Now open the solution with your preferred editor.

Rename the default C# file called *Class1.cs* to *ShortUrl.cs*, and add the following C# code to *ShortUrl.cs*:

**UrlShortener.Data/ShortUrl.cs**

```csharp
namespace UrlShortener.Data;

public sealed record ShortUrl(string? Destination, string? Path);
```

This record will hold on to the `Destination` and the `Path`. `Path` is the unique key that will be matched with the path from the incoming HTTP request in the Forwarder App, and `Destination` will be the URL the Forwarder App will forward the user to.

Next, create the *ShortUrlValidator.cs* file which will have code to validate the `ShortUrl` and its properties. Then, add the following C# code to the file:

**UrlShortener.Data/ShortUrlValidator.cs**

```csharp
using System.Text.RegularExpressions;

namespace UrlShortener.Data;

public static class ShortUrlValidator
{
    private static readonly Regex PathRegex = new Regex(
        "^[a-zA-Z0-9_-]*$",
        RegexOptions.None,
        TimeSpan.FromMilliseconds(1)
    );

    public static bool Validate(this ShortUrl shortUrl, out IDictionary<string, string[]> validationResults)
    {
        validationResults = new Dictionary<string, string[]>();
        var isDestinationValid = ValidateDestination(
            shortUrl.Destination,
            out var destinationValidationResults
        );
        var isPathValid = ValidatePath(
            shortUrl.Path,
            out var pathValidationResults
        );

        validationResults.Add("destination", destinationValidationResults);
        validationResults.Add("path", pathValidationResults);

        return isDestinationValid && isPathValid;
    }

    public static bool ValidateDestination(string? destination, out string[] validationResults)
    {
        if (destination == null)
        {
            validationResults = new[] {"Destination cannot be null."};
            return false;
        }

        if (destination == "")
        {
            validationResults = new[] {"Destination cannot empty."};
            return false;
        }

        if (!Uri.IsWellFormedUriString(destination, UriKind.Absolute))
        {
            validationResults = new[] {"Destination has to be a valid absolute URL."};
            return false;
        }

        validationResults = Array.Empty<string>();
        return true;
    }

    public static bool ValidatePath(string? path, out string[] validationResults)
    {
        if (path == null)
        {
            validationResults = new[] {"Path cannot be null."};
            return false;
        }

        if (path == "")
        {
            validationResults = new[] {"Path cannot empty."};
            return false;
        }

        var validationResultsList = new List<string>();
        if (path.Length > 10)
            validationResultsList.Add("Path cannot be longer than 10 characters.");

        if (!PathRegex.IsMatch(path))
            validationResultsList.Add("Path can only contain alphanumeric characters, underscores, and dashes.");
        
        validationResults = validationResultsList.ToArray();
        return validationResultsList.Count > 0;
    }
}
```

`ShortUrlValidator` has methods for validating the `ShortUrl` record and for its individual properties. For each validation rule that is not met, a string is added to the `validationResults` list. The `validationResults` are then added together into a dictionary. These are:

-   The `ShortUrl.Path` property can not be null or empty, not be longer than 10 characters, and has to match the `PathRegex` which only allows alphanumeric characters, underscores, and dashes.
-   The `ShortUrl.Destination` property can not be null or empty, and has to be a well formed absolute URL, meaning a URL of format `<scheme>://<hostname>:<port>` and optionally a path, query, and fragment.

These validation rules will be used in both the Forwarder App and the CLI CRUD App.

If you don't want to manually write your validation code, there are some great libraries out there that can help such as [FluentValidation](https://docs.fluentvalidation.net/en/latest/) for any type of project, and [MiniValidation](https://github.com/DamianEdwards/MiniValidation) to get the same validation experience as in ASP.NET Core MVC.

Now, the most important responsibility of this project is to manage the data in the Redis database. There's a great library for .NET by StackExchange, the StackOverflow company, for interacting with Redis databases called [StackExchange.Redis](https://stackexchange.github.io/StackExchange.Redis/). Add the [StackExchange.Redis NuGet package](https://www.nuget.org/packages/StackExchange.Redis) to the data project using the .NET CLI:

```bash
dotnet add UrlShortener.Data package StackExchange.Redis
```

Now, create a new file *ShortUrlRepository.cs* and add the following C# code:

**UrlShortener.Data/ShortUrlRepository.cs**

```csharp
namespace UrlShortener.Data;

using StackExchange.Redis;

public sealed class ShortUrlRepository
{
    private readonly ConnectionMultiplexer redisConnection;
    private readonly IDatabase redisDatabase;

    public ShortUrlRepository(ConnectionMultiplexer redisConnection)
    {
        this.redisConnection = redisConnection;
        this.redisDatabase = redisConnection.GetDatabase();
    }

    public async Task Create(ShortUrl shortUrl)
    {
        if (await Exists(shortUrl.Path))
            throw new Exception($"Shortened URL with path '{shortUrl.Path}' already exists.");

        var urlWasSet = await redisDatabase.StringSetAsync(shortUrl.Path, shortUrl.Destination);
        if (!urlWasSet)
            throw new Exception($"Failed to create shortened URL.");
    }

    public async Task Update(ShortUrl shortUrl)
    {
        if (await Exists(shortUrl.Path) == false)
            throw new Exception($"Shortened URL with path '{shortUrl.Path}' does not exist.");

        var urlWasSet = await redisDatabase.StringSetAsync(shortUrl.Path, shortUrl.Destination);
        if (!urlWasSet)
            throw new Exception($"Failed to update shortened URL.");
    }

    public async Task Delete(string path)
    {
        if (await Exists(path) == false)
            throw new Exception($"Shortened URL with path '{path}' does not exist.");

        var urlWasDeleted = await redisDatabase.KeyDeleteAsync(path);
        if (!urlWasDeleted)
            throw new Exception("Failed to delete shortened URL.");
    }

    public async Task<ShortUrl?> Get(string path)
    {
        if (await Exists(path) == false)
            throw new Exception($"Shortened URL with path '{path}' does not exist.");

        var redisValue = await redisDatabase.StringGetAsync(path);
        if (redisValue.IsNullOrEmpty)
            return null;
        
        return new ShortUrl(redisValue.ToString(), path);
    }

    public async Task<List<ShortUrl>> GetAll()
    {
        var redisServers = redisConnection.GetServers();
        var keys = new List<string>();
        foreach (var redisServer in redisServers)
        {
            await foreach (var redisKey in redisServer.KeysAsync())
            {
                var key = redisKey.ToString();
                if (keys.Contains(key)) continue;
                keys.Add(key);
            }
        }

        var redisDb = redisConnection.GetDatabase();

        var shortUrls = new List<ShortUrl>();
        foreach (var key in keys)
        {
            var redisValue = redisDb.StringGet(key);
            shortUrls.Add(new ShortUrl(redisValue.ToString(), key));
        }

        return shortUrls;
    }

    public async Task<bool> Exists(string? path)
        => await redisDatabase.KeyExistsAsync(path);
}
```

The `ShortUrlRepository` is responsible for interacting with the Redis database to manage the shortened URLs. `ShortUrlRepository` accepts an instance of `ConnectionMultiplexer` via its constructor which is used to get an `IDatabase` instance via `redisConnection.GetDatabase()`. `ConnectionMultiplexer` takes care of connecting to the Redis database, while the `IDatabase` class provides the Redis commands as .NET methods.

The `ShortUrlRepository` has the following methods:

-   `Create` to create a `ShortUrl` in the Redis database with the key being the path of the shortened URL, and the value being the destination URL.
-   `Update` to update an existing `ShortUrl` in the Redis database.
-   `Delete` to delete a `ShortUrl` by deleting the key in the Redis database.
-   `Get` to get the `ShortUrl` via the path.
-   `GetAll` to retrieve all the shortened URLs by retrieving all keys from all the connected Redis servers and then retrieve the values from the Redis database.

This provides all the CRUD functionality for shortened URLs.

Now let's use these classes to create the CRUD CLI App.

## Build the Command-Line CRUD Application 

Create a new console project in your solution folder, and add the project to the solution:

```bash
dotnet new console -o UrlShortener.Cli
dotnet sln add UrlShortener.Cli
```

Then, add a reference from the CLI project to the data project:

```bash
dotnet add UrlShortener.Cli reference UrlShortener.Data
```

Now you can use the public classes from the data project in your CLI project.

Next, add the [StackExchange.Redis NuGet package](https://www.nuget.org/packages/StackExchange.Redis) to this project as well:

```bash
dotnet add UrlShortener.Cli package StackExchange.Redis
```

Now, add the [System.CommandLine NuGet package](https://www.nuget.org/packages/System.CommandLine):

```bash
dotnet add UrlShortener.Cli package System.CommandLine --version 2.0.0-beta4.22272.1
```

At the time of writing, there are only prerelease versions of this library available. Once this library is fully released, you'll be able to add it without having to explicitly specify the version or the `--prerelease` argument.

<div class="block-rich_text"><p>The <code>System.CommandLine</code> libraries provide common functionality for building CLI applications. You can define your arguments and commands in C# and the library will execute your commands, and generate help text, command completion, and more.</p><p>Open the <em>UrlShortener.Cli/Program.cs</em> file, and replace the code with the following C#:</p><figure class="code-block code-block--titled" data-code-block="" data-highlighter="pierre" data-language="csharp" data-code-title="UrlShortener.Cli/Program.cs" data-astro-cid-c6nuo4je=""><figcaption class="code-block__titlebar" data-astro-cid-c6nuo4je=""><span class="code-block__title" data-astro-cid-c6nuo4je="">UrlShortener.Cli/Program.cs</span><span class="visually-hidden" data-astro-cid-c6nuo4je=""> — C#</span></figcaption><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">C#</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy UrlShortener.Cli/Program.cs — C# to clipboard" title="Copy UrlShortener.Cli/Program.cs — C# to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="UrlShortener.Cli/Program.cs — C#" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">using </span><span style="color:#702c00;--hls-dark:#ffb757">System</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">CommandLine</span><span style="color:#010409;--hls-dark:#ffffff">;
</span><span style="color:#a0111f;--hls-dark:#ff9492">using </span><span style="color:#702c00;--hls-dark:#ffb757">StackExchange</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Redis</span><span style="color:#010409;--hls-dark:#ffffff">;
</span><span style="color:#a0111f;--hls-dark:#ff9492">using </span><span style="color:#702c00;--hls-dark:#ffb757">UrlShortener</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Data</span><span style="color:#010409;--hls-dark:#ffffff">;

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">destinationOption </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#702c00;--hls-dark:#ffb757">Option</span><span style="color:#a0111f;--hls-dark:#ff9492">&lt;</span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#a0111f;--hls-dark:#ff9492">&gt;</span><span style="color:#010409;--hls-dark:#ffffff">(
    </span><span style="color:#a0111f;--hls-dark:#ff9492">new</span><span style="color:#010409;--hls-dark:#ffffff">[] {</span><span style="color:#032563;--hls-dark:#addcff">"--destination-url"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"-d"</span><span style="color:#010409;--hls-dark:#ffffff">},
    description: </span><span style="color:#032563;--hls-dark:#addcff">"The URL that the shortened URL will forward to."
</span><span style="color:#010409;--hls-dark:#ffffff">);
destinationOption.</span><span style="color:#702c00;--hls-dark:#ffb757">IsRequired </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#023b95;--hls-dark:#91cbff">true</span><span style="color:#010409;--hls-dark:#ffffff">;
destinationOption.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddValidator</span><span style="color:#010409;--hls-dark:#ffffff">(result </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">destination </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#010409;--hls-dark:#ffffff">result.</span><span style="color:#702c00;--hls-dark:#ffb757">Tokens</span><span style="color:#010409;--hls-dark:#ffffff">[</span><span style="color:#023b95;--hls-dark:#91cbff">0</span><span style="color:#010409;--hls-dark:#ffffff">].</span><span style="color:#702c00;--hls-dark:#ffb757">Value</span><span style="color:#010409;--hls-dark:#ffffff">;
    </span><span style="color:#a0111f;--hls-dark:#ff9492">if </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">ShortUrlValidator</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">ValidateDestination</span><span style="color:#010409;--hls-dark:#ffffff">(destination, </span><span style="color:#a0111f;--hls-dark:#ff9492">out var </span><span style="color:#010409;--hls-dark:#ffffff">validationResults) </span><span style="color:#a0111f;--hls-dark:#ff9492">== </span><span style="color:#023b95;--hls-dark:#91cbff">false</span><span style="color:#010409;--hls-dark:#ffffff">)
    {
        result.</span><span style="color:#702c00;--hls-dark:#ffb757">ErrorMessage </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Join</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">", "</span><span style="color:#010409;--hls-dark:#ffffff">, validationResults);
    }
});

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">pathOption </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#702c00;--hls-dark:#ffb757">Option</span><span style="color:#a0111f;--hls-dark:#ff9492">&lt;</span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#a0111f;--hls-dark:#ff9492">&gt;</span><span style="color:#010409;--hls-dark:#ffffff">(
    </span><span style="color:#a0111f;--hls-dark:#ff9492">new</span><span style="color:#010409;--hls-dark:#ffffff">[] {</span><span style="color:#032563;--hls-dark:#addcff">"--path"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"-p"</span><span style="color:#010409;--hls-dark:#ffffff">},
    description: </span><span style="color:#032563;--hls-dark:#addcff">"The path used for the shortened URL."
</span><span style="color:#010409;--hls-dark:#ffffff">);
pathOption.</span><span style="color:#702c00;--hls-dark:#ffb757">IsRequired </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#023b95;--hls-dark:#91cbff">true</span><span style="color:#010409;--hls-dark:#ffffff">;
pathOption.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddValidator</span><span style="color:#010409;--hls-dark:#ffffff">(result </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">path </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#010409;--hls-dark:#ffffff">result.</span><span style="color:#702c00;--hls-dark:#ffb757">Tokens</span><span style="color:#010409;--hls-dark:#ffffff">[</span><span style="color:#023b95;--hls-dark:#91cbff">0</span><span style="color:#010409;--hls-dark:#ffffff">].</span><span style="color:#702c00;--hls-dark:#ffb757">Value</span><span style="color:#010409;--hls-dark:#ffffff">;
    </span><span style="color:#a0111f;--hls-dark:#ff9492">if </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">ShortUrlValidator</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">ValidatePath</span><span style="color:#010409;--hls-dark:#ffffff">(path, </span><span style="color:#a0111f;--hls-dark:#ff9492">out var </span><span style="color:#010409;--hls-dark:#ffffff">validationResults) </span><span style="color:#a0111f;--hls-dark:#ff9492">== </span><span style="color:#023b95;--hls-dark:#91cbff">false</span><span style="color:#010409;--hls-dark:#ffffff">)
    {
        result.</span><span style="color:#702c00;--hls-dark:#ffb757">ErrorMessage </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Join</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">", "</span><span style="color:#010409;--hls-dark:#ffffff">, validationResults);
    }
});

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">connectionStringOption </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#702c00;--hls-dark:#ffb757">Option</span><span style="color:#a0111f;--hls-dark:#ff9492">&lt;</span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#a0111f;--hls-dark:#ff9492">?&gt;</span><span style="color:#010409;--hls-dark:#ffffff">(
    </span><span style="color:#a0111f;--hls-dark:#ff9492">new</span><span style="color:#010409;--hls-dark:#ffffff">[] {</span><span style="color:#032563;--hls-dark:#addcff">"--connection-string"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"-c"</span><span style="color:#010409;--hls-dark:#ffffff">},
    description: </span><span style="color:#032563;--hls-dark:#addcff">"Connection string to connect to the Redis Database where URLs are stored. " </span><span style="color:#a0111f;--hls-dark:#ff9492">+
                 </span><span style="color:#032563;--hls-dark:#addcff">"Alternatively, you can set the 'URL_SHORTENER_CONNECTION_STRING'."
</span><span style="color:#010409;--hls-dark:#ffffff">);
</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">envConnectionString </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#702c00;--hls-dark:#ffb757">Environment</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetEnvironmentVariable</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"URL_SHORTENER_CONNECTION_STRING"</span><span style="color:#010409;--hls-dark:#ffffff">);
</span><span style="color:#a0111f;--hls-dark:#ff9492">if </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">IsNullOrEmpty</span><span style="color:#010409;--hls-dark:#ffffff">(envConnectionString))
{
    connectionStringOption.</span><span style="color:#702c00;--hls-dark:#ffb757">IsRequired </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#023b95;--hls-dark:#91cbff">true</span><span style="color:#010409;--hls-dark:#ffffff">;
}
</span></code></pre></figure><p>This code defines the options that you can pass into the CLI application:</p><ul><li><code>destinationOption</code> can be passed in using <code>-d</code> or <code>--destination-url</code> and is required.</li><li><code>pathOption</code> can be passed in using <code>-p</code> or <code>--path</code> and is also required.</li><li><code>connectionStringOption</code> can be passed in using <code>-c</code> or <code>--connection-string</code>. Alternatively, you can set the <code>URL_SHORTENER_CONNECTION_STRING</code> environment variable which is preferred over passing sensitive strings as an argument. If the <code>URL_SHORTENER_CONNECTION_STRING</code> environment is not present, then the <code>connectionStringOption</code> is required.</li></ul><p>The <code>destinationOption</code> and <code>pathOption</code> validate the argument by passing in the argument value to their respective <code>ShortUrlValidator</code> methods. The validation results are then concatenated and set to <code>result.ErrorMessage</code> which will be displayed as errors to the user.</p><p>Now that the options are defined, you can create commands that receive the options.<br>Add the following code after your existing code:</p><figure class="code-block code-block--titled" data-code-block="" data-highlighter="pierre" data-language="csharp" data-code-title="UrlShortener.Cli/Program.cs" data-astro-cid-c6nuo4je=""><figcaption class="code-block__titlebar" data-astro-cid-c6nuo4je=""><span class="code-block__title" data-astro-cid-c6nuo4je="">UrlShortener.Cli/Program.cs</span><span class="visually-hidden" data-astro-cid-c6nuo4je=""> — C#</span></figcaption><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">C#</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy UrlShortener.Cli/Program.cs — C# to clipboard" title="Copy UrlShortener.Cli/Program.cs — C# to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="UrlShortener.Cli/Program.cs — C#" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">rootCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">RootCommand</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Manage the shortened URLs."</span><span style="color:#010409;--hls-dark:#ffffff">);

</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#702c00;--hls-dark:#ffb757">Task</span><span style="color:#a0111f;--hls-dark:#ff9492">&lt;</span><span style="color:#702c00;--hls-dark:#ffb757">ConnectionMultiplexer</span><span style="color:#a0111f;--hls-dark:#ff9492">&gt; </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#a0111f;--hls-dark:#ff9492">? </span><span style="color:#010409;--hls-dark:#ffffff">connectionString)
{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">redisConnection </span><span style="color:#a0111f;--hls-dark:#ff9492">= await </span><span style="color:#702c00;--hls-dark:#ffb757">ConnectionMultiplexer</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">ConnectAsync</span><span style="color:#010409;--hls-dark:#ffffff">(
        connectionString </span><span style="color:#a0111f;--hls-dark:#ff9492">??
        </span><span style="color:#010409;--hls-dark:#ffffff">envConnectionString </span><span style="color:#a0111f;--hls-dark:#ff9492">??
        throw new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Exception</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Missing connection string."</span><span style="color:#010409;--hls-dark:#ffffff">)
    );
    </span><span style="color:#a0111f;--hls-dark:#ff9492">return </span><span style="color:#010409;--hls-dark:#ffffff">redisConnection;
}

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">createCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Command</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"create"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"Create a shortened URL"</span><span style="color:#010409;--hls-dark:#ffffff">)
{
    destinationOption,
    pathOption,
    connectionStringOption
};

createCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">SetHandler</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(destination, path, connectionString) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrlRepository</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString));
    </span><span style="color:#a0111f;--hls-dark:#ff9492">try
    </span><span style="color:#010409;--hls-dark:#ffffff">{
        </span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Create</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrl</span><span style="color:#010409;--hls-dark:#ffffff">(destination, path));
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Shortened URL created."</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
    </span><span style="color:#a0111f;--hls-dark:#ff9492">catch </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">Exception </span><span style="color:#010409;--hls-dark:#ffffff">e)
    {
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(e.</span><span style="color:#702c00;--hls-dark:#ffb757">Message</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
}, destinationOption, pathOption, connectionStringOption);

rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddCommand</span><span style="color:#010409;--hls-dark:#ffffff">(createCommand);
</span></code></pre></figure><p>The <code>rootCommand</code> is the command that is invoked when you invoke the CLI application without passing any subcommands. The <code>create</code> command takes in the <code>destinationOption</code>, <code>pathOption</code>, and <code>connectionStringOption</code>. The lambda passed into the <code>SetHandler</code> method receives the values for the options and will be executed when the command is run from the CLI.</p><p>Since the <code>connectionStringOption</code> may not be required, the <code>connectionString</code> parameter may be null. The <code>GetRedisConnection</code> method checks if it is null, and if so, returns the value from the environment variables. If that is also null, it'll throw an exception.</p><p>The command handler will create a new <code>ShortUrlRepository</code> passing in the connection string, and use the <code>Create</code> method to create a new shortened URL.</p><p>Now, add the following code which will add the <code>update</code>, <code>delete</code>, <code>get</code>, and <code>list</code> commands:</p><figure class="code-block code-block--titled" data-code-block="" data-highlighter="pierre" data-language="csharp" data-code-title="UrlShortener.Cli/Program.cs" data-astro-cid-c6nuo4je=""><figcaption class="code-block__titlebar" data-astro-cid-c6nuo4je=""><span class="code-block__title" data-astro-cid-c6nuo4je="">UrlShortener.Cli/Program.cs</span><span class="visually-hidden" data-astro-cid-c6nuo4je=""> — C#</span></figcaption><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">C#</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy UrlShortener.Cli/Program.cs — C# to clipboard" title="Copy UrlShortener.Cli/Program.cs — C# to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="UrlShortener.Cli/Program.cs — C#" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">updateCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Command</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"update"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"Update a shortened URL"</span><span style="color:#010409;--hls-dark:#ffffff">)
{
    destinationOption,
    pathOption,
    connectionStringOption
};

updateCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">SetHandler</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(destination, path, connectionString) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrlRepository</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString));
    </span><span style="color:#a0111f;--hls-dark:#ff9492">try
    </span><span style="color:#010409;--hls-dark:#ffffff">{
        </span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Update</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrl</span><span style="color:#010409;--hls-dark:#ffffff">(destination, path));
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Shortened URL updated."</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
    </span><span style="color:#a0111f;--hls-dark:#ff9492">catch </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">Exception </span><span style="color:#010409;--hls-dark:#ffffff">e)
    {
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(e.</span><span style="color:#702c00;--hls-dark:#ffb757">Message</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
}, destinationOption, pathOption, connectionStringOption);

rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddCommand</span><span style="color:#010409;--hls-dark:#ffffff">(updateCommand);

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">deleteCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Command</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"delete"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"Delete a shortened URL"</span><span style="color:#010409;--hls-dark:#ffffff">)
{
    pathOption,
    connectionStringOption
};

deleteCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">SetHandler</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(path, connectionString) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrlRepository</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString));
    </span><span style="color:#a0111f;--hls-dark:#ff9492">try
    </span><span style="color:#010409;--hls-dark:#ffffff">{
        </span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Delete</span><span style="color:#010409;--hls-dark:#ffffff">(path);
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Shortened URL deleted."</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
    </span><span style="color:#a0111f;--hls-dark:#ff9492">catch </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">Exception </span><span style="color:#010409;--hls-dark:#ffffff">e)
    {
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(e.</span><span style="color:#702c00;--hls-dark:#ffb757">Message</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
}, pathOption, connectionStringOption);

rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddCommand</span><span style="color:#010409;--hls-dark:#ffffff">(deleteCommand);

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">getCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Command</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"get"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"Get a shortened URL"</span><span style="color:#010409;--hls-dark:#ffffff">)
{
    pathOption,
    connectionStringOption
};

getCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">SetHandler</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(path, connectionString) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrlRepository</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString));
    </span><span style="color:#a0111f;--hls-dark:#ff9492">try
    </span><span style="color:#010409;--hls-dark:#ffffff">{
        </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrl </span><span style="color:#a0111f;--hls-dark:#ff9492">= await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Get</span><span style="color:#010409;--hls-dark:#ffffff">(path);
        </span><span style="color:#a0111f;--hls-dark:#ff9492">if </span><span style="color:#010409;--hls-dark:#ffffff">(shortUrl </span><span style="color:#a0111f;--hls-dark:#ff9492">== </span><span style="color:#023b95;--hls-dark:#91cbff">null</span><span style="color:#010409;--hls-dark:#ffffff">)
            </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">$"Shortened URL for path '</span><span style="color:#a0111f;--hls-dark:#ff9492">{</span><span style="color:#010409;--hls-dark:#ffffff">path</span><span style="color:#a0111f;--hls-dark:#ff9492">}</span><span style="color:#032563;--hls-dark:#addcff">' not found."</span><span style="color:#010409;--hls-dark:#ffffff">);
        </span><span style="color:#a0111f;--hls-dark:#ff9492">else
            </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">$"Destination URL: </span><span style="color:#a0111f;--hls-dark:#ff9492">{</span><span style="color:#010409;--hls-dark:#ffffff">shortUrl.</span><span style="color:#702c00;--hls-dark:#ffb757">Destination</span><span style="color:#a0111f;--hls-dark:#ff9492">}</span><span style="color:#032563;--hls-dark:#addcff">, Path: </span><span style="color:#a0111f;--hls-dark:#ff9492">{</span><span style="color:#010409;--hls-dark:#ffffff">path</span><span style="color:#a0111f;--hls-dark:#ff9492">}</span><span style="color:#032563;--hls-dark:#addcff">"</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
    </span><span style="color:#a0111f;--hls-dark:#ff9492">catch </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">Exception </span><span style="color:#010409;--hls-dark:#ffffff">e)
    {
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(e.</span><span style="color:#702c00;--hls-dark:#ffb757">Message</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
}, pathOption, connectionStringOption);

rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddCommand</span><span style="color:#010409;--hls-dark:#ffffff">(getCommand);

</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">listCommand </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Command</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"list"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#032563;--hls-dark:#addcff">"List shortened URLs"</span><span style="color:#010409;--hls-dark:#ffffff">)
{
    connectionStringOption
};

listCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">SetHandler</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(connectionString) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;
</span><span style="color:#010409;--hls-dark:#ffffff">{
    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository </span><span style="color:#a0111f;--hls-dark:#ff9492">= new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">ShortUrlRepository</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">await </span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetRedisConnection</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString));
    </span><span style="color:#a0111f;--hls-dark:#ff9492">try
    </span><span style="color:#010409;--hls-dark:#ffffff">{
        </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrls </span><span style="color:#a0111f;--hls-dark:#ff9492">= await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetAll</span><span style="color:#010409;--hls-dark:#ffffff">();
        </span><span style="color:#a0111f;--hls-dark:#ff9492">foreach </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrl </span><span style="color:#a0111f;--hls-dark:#ff9492">in </span><span style="color:#010409;--hls-dark:#ffffff">shortUrls)
        {
            </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">$"Destination URL: </span><span style="color:#a0111f;--hls-dark:#ff9492">{</span><span style="color:#010409;--hls-dark:#ffffff">shortUrl.</span><span style="color:#702c00;--hls-dark:#ffb757">Destination</span><span style="color:#a0111f;--hls-dark:#ff9492">}</span><span style="color:#032563;--hls-dark:#addcff">, Path: </span><span style="color:#a0111f;--hls-dark:#ff9492">{</span><span style="color:#010409;--hls-dark:#ffffff">shortUrl.</span><span style="color:#702c00;--hls-dark:#ffb757">Path</span><span style="color:#a0111f;--hls-dark:#ff9492">}</span><span style="color:#032563;--hls-dark:#addcff">"</span><span style="color:#010409;--hls-dark:#ffffff">);
        }
    }
    </span><span style="color:#a0111f;--hls-dark:#ff9492">catch </span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">Exception </span><span style="color:#010409;--hls-dark:#ffffff">e)
    {
        </span><span style="color:#702c00;--hls-dark:#ffb757">Console</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Error</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">WriteLine</span><span style="color:#010409;--hls-dark:#ffffff">(e.</span><span style="color:#702c00;--hls-dark:#ffb757">Message</span><span style="color:#010409;--hls-dark:#ffffff">);
    }
}, connectionStringOption);

rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddCommand</span><span style="color:#010409;--hls-dark:#ffffff">(listCommand);
</span></code></pre></figure><p>Note how some commands take less options than others.</p><p>Lastly, add the following line of code:</p><figure class="code-block code-block--titled" data-code-block="" data-highlighter="pierre" data-language="csharp" data-code-title="UrlShortener.Cli/Program.cs" data-astro-cid-c6nuo4je=""><figcaption class="code-block__titlebar" data-astro-cid-c6nuo4je=""><span class="code-block__title" data-astro-cid-c6nuo4je="">UrlShortener.Cli/Program.cs</span><span class="visually-hidden" data-astro-cid-c6nuo4je=""> — C#</span></figcaption><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">C#</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy UrlShortener.Cli/Program.cs — C# to clipboard" title="Copy UrlShortener.Cli/Program.cs — C# to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="UrlShortener.Cli/Program.cs — C#" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">return </span><span style="color:#010409;--hls-dark:#ffffff">rootCommand.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">InvokeAsync</span><span style="color:#010409;--hls-dark:#ffffff">(args).</span><span style="color:#702c00;--hls-dark:#ffb757">Result</span><span style="color:#010409;--hls-dark:#ffffff">;
</span></code></pre></figure><p>This line is responsible for running the commands and passing in the <code>args</code> string array.</p><p>Go back to your terminal and configure the <code>URL_SHORTENER_CONNECTION_STRING</code> environment variable:</p><p>If you use PowerShell:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="powershell" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">PowerShell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy PowerShell code to clipboard" title="Copy PowerShell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="PowerShell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#010409;--hls-dark:#ffffff">$Env:URL_SHORTENER_CONNECTION_STRING </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#032563;--hls-dark:#addcff">'[YOUR_CONNECTION_STRING]'
</span></code></pre></figure><p>If you use CMD:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="batch" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">batch</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy batch code to clipboard" title="Copy batch code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="batch code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">set </span><span style="color:#010409;--hls-dark:#ffffff">URL_SHORTENER_CONNECTION_STRING</span><span style="color:#a0111f;--hls-dark:#ff9492">=</span>[<span style="color:#010409;--hls-dark:#ffffff">YOUR_CONNECTION_STRING</span>]
</code></pre></figure><p>If you use Unix based shells such as Bash or Zsh:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#a0111f;--hls-dark:#ff9492">export </span><span style="color:#010409;--hls-dark:#ffffff">URL_SHORTENER_CONNECTION_STRING</span><span style="color:#a0111f;--hls-dark:#ff9492">=</span><span style="color:#010409;--hls-dark:#ffffff">[YOUR_CONNECTION_STRING]
</span></code></pre></figure><p>Replace <code>[YOUR_CONNECTION_STRING]</code> with the connection string pointing to your Redis server.</p><p>Now you can run the project and will see helpful information about the available commands:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli
</span></code></pre></figure><p>The output looks like this:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="text" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Plain text</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Plain text code to clipboard" title="Copy Plain text code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Plain text code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no">Required command was not provided.

Description:
  Manage the shortened URLs.

Usage:
  UrlShortener.Cli [command] [options]

Options:
  --version       Show version information
  -?, -h, --help  Show help and usage information

Commands:
  create  Create a shortened URL
  update  Update a shortened URL
  delete  Delete a shortened URL
  get     Get a shortened URL
  list    List shortened URLs
</code></pre></figure><p>To get help information about specific commands, specify the command and add the <code>--help</code> argument:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- create --help
</span></code></pre></figure><p>The output looks like this:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="text" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Plain text</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Plain text code to clipboard" title="Copy Plain text code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Plain text code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no">Description:
  Create a shortened URL

Usage:
  UrlShortener.Cli create [options]

Options:
  -d, --destination-url &lt;destination-url&gt;      The URL that the shortened URL will forward to.
  (REQUIRED)
  -p, --path &lt;path&gt; (REQUIRED)                 The path used for the shortened URL.
  -c, --connection-string &lt;connection-string&gt;  Connection string to connect to the Redis Database
                                               where URLs are stored. Alternatively, you can set
                                               the 'URL_SHORTENER_CONNECTION_STRING'.
  -?, -h, --help                               Show help and usage information
</code></pre></figure><p>Next, try the following commands:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- create -d https</span>:<span style="color:#010409;--hls-dark:#ffffff">//www.youtube.com/watch</span>?<span style="color:#010409;--hls-dark:#ffffff">v</span><span style="color:#a0111f;--hls-dark:#ff9492">=</span><span style="color:#010409;--hls-dark:#ffffff">dQw4w9WgXcQ -p rr
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- create -d https</span>:<span style="color:#010409;--hls-dark:#ffffff">//www.twilio.com -p tw
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- create -d https</span>:<span style="color:#010409;--hls-dark:#ffffff">//sendgrid.com -p sg
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- create -d https</span>:<span style="color:#010409;--hls-dark:#ffffff">//swimburger.net -p sb
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- get -p sb
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- update -d https</span>:<span style="color:#010409;--hls-dark:#ffffff">//swimburger.net/blog -p sb
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Cli -- list
</span></code></pre></figure><p>Alternatively, you could publish the project and interact directly with the executable:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">publish UrlShortener.Cli -o publish
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">cd </span><span style="color:#010409;--hls-dark:#ffffff">publish
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">./UrlShortener.Cli </span><span style="color:#010409;--hls-dark:#ffffff">list 
</span></code></pre></figure><p>Great job! You built the CRUD as a CLI application, now let's build the Forwarder Application.</p><h2 id="build-the-aspnet-core-forwarder-application">Build the ASP.NET Core Forwarder Application&nbsp;</h2><p>In your terminal, head back to the solution folder and then create a new ASP.NET Core Minimal API project:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">cd </span><span style="color:#010409;--hls-dark:#ffffff">..
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">new web -o UrlShortener.Forwarder
</span></code></pre></figure><p>Just like with the CRUD CLI project, add a reference in the Forwarder project to the data project, and add the <a href="https://www.nuget.org/packages/StackExchange.Redis">StackExchange.Redis NuGet package</a>:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">add UrlShortener.Forwarder reference UrlShortener.Data
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">add UrlShortener.Forwarder package StackExchange.Redis
</span></code></pre></figure><p>Now, open <em>UrlShortener.Forwarder/Program.cs</em> and replace the contents with the following code:</p><figure class="code-block code-block--titled" data-code-block="" data-highlighter="pierre" data-language="csharp" data-code-title="UrlShortener.Forwarder/Program.cs" data-highlight-lines="6-10" data-astro-cid-c6nuo4je=""><figcaption class="code-block__titlebar" data-astro-cid-c6nuo4je=""><span class="code-block__title" data-astro-cid-c6nuo4je="">UrlShortener.Forwarder/Program.cs</span><span class="visually-hidden" data-astro-cid-c6nuo4je=""> — C#</span></figcaption><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">C#</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy UrlShortener.Forwarder/Program.cs — C# to clipboard" title="Copy UrlShortener.Forwarder/Program.cs — C# to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre data-highlight-lines="6-10" aria-keyshortcuts="Control+C Meta+C" aria-label="UrlShortener.Forwarder/Program.cs — C#" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code data-trailing-newline="" data-code-lines="" spellcheck="false" translate="no"><span class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">using </span><span style="color:#702c00;--hls-dark:#ffb757">StackExchange</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Redis</span><span style="color:#010409;--hls-dark:#ffffff">;</span></span><span class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">using </span><span style="color:#702c00;--hls-dark:#ffb757">UrlShortener</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">Data</span><span style="color:#010409;--hls-dark:#ffffff">;</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">builder </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#702c00;--hls-dark:#ffb757">WebApplication</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">CreateBuilder</span><span style="color:#010409;--hls-dark:#ffffff">(args);</span></span><span class="code-block__line"></span><span data-highlighted-line="" class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">connectionString </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#010409;--hls-dark:#ffffff">builder.</span><span style="color:#702c00;--hls-dark:#ffb757">Configuration</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">GetConnectionString</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"ShortenedUrlsDb"</span><span style="color:#010409;--hls-dark:#ffffff">)</span></span><span data-highlighted-line="" class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">            </span><span style="color:#a0111f;--hls-dark:#ff9492">?? throw new </span><span style="color:#622cbc;--hls-dark:#dbb7ff">Exception</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"Missing 'ShortenedUrlsDb' connection string"</span><span style="color:#010409;--hls-dark:#ffffff">);</span></span><span data-highlighted-line="" class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">redisConnection </span><span style="color:#a0111f;--hls-dark:#ff9492">= await </span><span style="color:#702c00;--hls-dark:#ffb757">ConnectionMultiplexer</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">ConnectAsync</span><span style="color:#010409;--hls-dark:#ffffff">(connectionString);</span></span><span data-highlighted-line="" class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">builder.</span><span style="color:#702c00;--hls-dark:#ffb757">Services</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">AddSingleton</span><span style="color:#010409;--hls-dark:#ffffff">(redisConnection);</span></span><span data-highlighted-line="" class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">builder.</span><span style="color:#702c00;--hls-dark:#ffb757">Services</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#702c00;--hls-dark:#ffb757">AddTransient</span><span style="color:#a0111f;--hls-dark:#ff9492">&lt;</span><span style="color:#702c00;--hls-dark:#ffb757">ShortUrlRepository</span><span style="color:#a0111f;--hls-dark:#ff9492">&gt;</span><span style="color:#010409;--hls-dark:#ffffff">();</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">app </span><span style="color:#a0111f;--hls-dark:#ff9492">= </span><span style="color:#010409;--hls-dark:#ffffff">builder.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Build</span><span style="color:#010409;--hls-dark:#ffffff">();</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">app.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">MapGet</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#032563;--hls-dark:#addcff">"/{path}"</span><span style="color:#010409;--hls-dark:#ffffff">, </span><span style="color:#a0111f;--hls-dark:#ff9492">async </span><span style="color:#010409;--hls-dark:#ffffff">(</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#023b95;--hls-dark:#91cbff">string </span><span style="color:#010409;--hls-dark:#ffffff">path,</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#702c00;--hls-dark:#ffb757">ShortUrlRepository </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">) </span><span style="color:#a0111f;--hls-dark:#ff9492">=&gt;</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">{</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#a0111f;--hls-dark:#ff9492">if</span><span style="color:#010409;--hls-dark:#ffffff">(</span><span style="color:#702c00;--hls-dark:#ffb757">ShortUrlValidator</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">ValidatePath</span><span style="color:#010409;--hls-dark:#ffffff">(path, </span><span style="color:#a0111f;--hls-dark:#ff9492">out </span><span style="color:#010409;--hls-dark:#ffffff">_))</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">        </span><span style="color:#a0111f;--hls-dark:#ff9492">return </span><span style="color:#702c00;--hls-dark:#ffb757">Results</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">BadRequest</span><span style="color:#010409;--hls-dark:#ffffff">();</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#a0111f;--hls-dark:#ff9492">var </span><span style="color:#010409;--hls-dark:#ffffff">shortUrl </span><span style="color:#a0111f;--hls-dark:#ff9492">= await </span><span style="color:#010409;--hls-dark:#ffffff">shortUrlRepository.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Get</span><span style="color:#010409;--hls-dark:#ffffff">(path);</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#a0111f;--hls-dark:#ff9492">if </span><span style="color:#010409;--hls-dark:#ffffff">(shortUrl </span><span style="color:#a0111f;--hls-dark:#ff9492">== </span><span style="color:#023b95;--hls-dark:#91cbff">null </span><span style="color:#a0111f;--hls-dark:#ff9492">|| </span><span style="color:#023b95;--hls-dark:#91cbff">string</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">IsNullOrEmpty</span><span style="color:#010409;--hls-dark:#ffffff">(shortUrl.</span><span style="color:#702c00;--hls-dark:#ffb757">Destination</span><span style="color:#010409;--hls-dark:#ffffff">))</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">        </span><span style="color:#a0111f;--hls-dark:#ff9492">return </span><span style="color:#702c00;--hls-dark:#ffb757">Results</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">NotFound</span><span style="color:#010409;--hls-dark:#ffffff">();</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">    </span><span style="color:#a0111f;--hls-dark:#ff9492">return </span><span style="color:#702c00;--hls-dark:#ffb757">Results</span><span style="color:#010409;--hls-dark:#ffffff">.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Redirect</span><span style="color:#010409;--hls-dark:#ffffff">(shortUrl.</span><span style="color:#702c00;--hls-dark:#ffb757">Destination</span><span style="color:#010409;--hls-dark:#ffffff">);</span></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">});</span></span><span class="code-block__line"></span><span class="code-block__line"><span style="color:#010409;--hls-dark:#ffffff">app.</span><span style="color:#622cbc;--hls-dark:#dbb7ff">Run</span><span style="color:#010409;--hls-dark:#ffffff">();</span></span></code></pre></figure><p>Let's look at lines 6 to 10 first. The program will retrieve the <code>UrlsDb</code> Redis connection string from the configuration, and if null, throw an exception. Then, a connection to the Redis server is made which is added to the Dependency Injection (DI) container as a singleton.</p><p>Next, the <code>ShortUrlRepository</code> is added as a transient service to the DI container. Since <code>ShortUrlRepository</code> accepts a <code>ConnectionMultiplexer</code> object via its constructor, the DI container is able to construct the <code>ShortUrlRepository</code> for you passing in the singleton <code>ConnectionMultiplexer</code>.</p><p>Next, let's look at lines 14 to 27. A new HTTP GET endpoint is added with the route <code>/{path}</code>. This endpoint will be invoked by any HTTP request with a single level path. Without a path, ASP.NET Core will return an HTTP status code of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404">404 Not Found</a>, and so will requests with subdirectory paths.</p><p>When the endpoint is invoked, the path of the URL is passed into the <code>path</code> parameter of the lambda. The second lambda parameter, an instance of <code>ShortUrlRepository</code>, is injected by the DI container.<br>The path is then validated using <code>ShortUrlValidator.ValidatePath</code>. If the path is not valid, an HTTP status code of <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400">400 Bad Request</a> is returned.</p><p>If it is valid, the shortened URL is retrieved using the <code>ShortUrlRepository.Get</code> method. If no shortened URL is found, <code>shortUrl</code> will be null. If the <code>shortUrl</code> is null, or when it is not null but the <code>ShortUrl.Destination</code> is null or empty, then an HTTP status code <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404">404 Not Found</a> is returned. Otherwise, the endpoint responds with HTTP status code <code>307 Temporary Redirect</code>, redirecting to the <code>ShortUrl.Destination</code>.</p><p>Now that your code is ready, you'll need to configure the <code>ShortenedUrlsDb</code> connection string. Run the following commands to enable user-secrets, and then configure your connection string as a user-secret:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">user-secrets --project UrlShortener.Forwarder init
</span><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">user-secrets --project UrlShortener.Forwarder set ConnectionStrings</span>:<span style="color:#010409;--hls-dark:#ffffff">ShortenedUrlsDb [YOUR_CONNECTION_STRING]
</span></code></pre></figure><p>Replace <code>[YOUR_CONNECTION_STRING]</code> with the connection string pointing to your Redis server.</p><p>Finally, run the Forwarder application:</p><figure class="code-block" data-code-block="" data-highlighter="pierre" data-language="bash" data-astro-cid-c6nuo4je=""><span class="code-block__language" aria-hidden="true" data-astro-cid-c6nuo4je="">Shell</span><button type="button" class="code-block__copy" data-copy-code="" aria-label="Copy Shell code to clipboard" title="Copy Shell code to clipboard" data-astro-cid-c6nuo4je=""><svg class="code-block__copy-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><rect x="8" y="8" width="11" height="11" rx="1.5" data-astro-cid-c6nuo4je=""></rect><path d="M16 8V6.5A1.5 1.5 0 0 0 14.5 5h-8A1.5 1.5 0 0 0 5 6.5v8A1.5 1.5 0 0 0 6.5 16H8" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__success-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m5 12 4 4L19 6" data-astro-cid-c6nuo4je=""></path></svg><svg class="code-block__error-icon" viewBox="0 0 24 24" aria-hidden="true" data-astro-cid-c6nuo4je=""><path d="m7 7 10 10M17 7 7 17" data-astro-cid-c6nuo4je=""></path></svg><span class="visually-hidden" data-copy-code-label="" data-astro-cid-c6nuo4je="">Copy</span></button><span class="visually-hidden" role="status" aria-live="polite" data-copy-code-status="" data-astro-cid-c6nuo4je=""></span><pre aria-keyshortcuts="Control+C Meta+C" aria-label="Shell code" tabindex="0" class="highlights code-block__pre" style="background-color:#ffffff;--hls-dark-bg:#010409;color:#010409;--hls-dark:#ffffff"><code spellcheck="false" translate="no"><span style="color:#622cbc;--hls-dark:#dbb7ff">dotnet </span><span style="color:#010409;--hls-dark:#ffffff">run --project UrlShortener.Forwarder
</span></code></pre></figure><p>The output of this command will show you the URLs of your application. Open one of the URLs and try some of the shortened URLs you created earlier, like <em>/rr</em>, <em>/sb</em>, <em>/tw</em>, and <em>/sg.</em></p><h2 id="next-steps">Next steps&nbsp;</h2><p>You've developed a CLI application to manage shortened URLs and a web application that forwards the shortened URLs to the destination URLs. However, to make the URL as short as possible, you need to also buy a domain that is short and host your URL shortener at that domain. For example, Twilio uses <em>twil.io</em> as a short domain, which is much shorter than <a href="http://www.twilio.com">www.twilio.com</a>. This is also a good example of how the name of your brand can still be represented in your short domain name.</p><p>There are some other ways you could improve the solution:</p><ul><li>You could make the <code>path</code> optional when creating and updating a shortened URL, and instead randomly generate the path.</li><li>You could store a time to live (TTL) for every shortened URL and remove the shortened URL when the TTL expires.</li><li>You could track how many times a shortened URL is used for analytics purposes. You could store this data in the Redis database, or <a href="https://segment.com/docs/connections/sources/catalog/libraries/server/net/#track">track it as an event in Segment</a>.</li><li>You could add an API that is consumable from other applications. For example, an SMS application could dynamically generate shortened URLs via the API for the long URLs they are trying to send to users.</li><li>You could create a Graphical User Interface (GUI) to manage the shortened URL data instead of a CLI application.</li></ul><div class="block-warning_danger"><div class="docs-note docs-note--info"><p>If you want to use shortened URLs with Twilio SMS, Twilio Messaging has a built-in feature called <a href="https://www.twilio.com/docs/messaging/services#link-shortening-click-tracking">Link Shortening &amp; Click Tracking</a>.</p><p>Want to keep learning? Learn <a href="https://www.twilio.com/blog/sms-voice-dotnet-6-minimal-api">how to respond to SMS and Voice calls using ASP.NET Core Minimal APIs</a>.</p></div></div></div>
