title | description | author | ms.author | ms.service | ms.devlang | ms.custom | ms.topic | ms.date |
---|---|---|---|---|---|---|---|---|
Create an ASP.NET Core web app with Azure Cache for Redis |
In this quickstart, you learn how to create an ASP.NET Core web app with Azure Cache for Redis |
flang-msft |
franlanglois |
cache |
csharp |
devx-track-csharp, mvc, mode-other |
quickstart |
03/25/2022 |
In this quickstart, you incorporate Azure Cache for Redis into an ASP.NET Core web application that connects to Azure Cache for Redis to store and retrieve data from the cache.
Clone the repo https://github.com/Azure-Samples/azure-cache-redis-samples/tree/main/quickstart/aspnet-core on GitHub.
- Azure subscription - create one for free
- .NET Core SDK
[!INCLUDE redis-cache-create]
[!INCLUDE redis-cache-access-keys]
Make a note of the HOST NAME and the Primary access key. You use these values later to construct the CacheConnection secret.
In your command window, execute the following command to store a new secret named CacheConnection, after replacing the placeholders, including angle brackets, for your cache name and primary access key:
dotnet user-secrets set CacheConnection "<cache name>.redis.cache.windows.net,abortConnect=false,ssl=true,allowAdmin=true,password=<primary-access-key>"
The connection to your cache is managed by the RedisConnection
class. The connection is made in this statement in HomeController.cs
in the Controllers folder:
_redisConnection = await _redisConnectionFactory;
In RedisConnection.cs
, you see the StackExchange.Redis
namespace has been added to the code. This is needed for the RedisConnection
class.
using StackExchange.Redis;
The RedisConnection
code ensures that there is always a healthy connection to the cache by managing the ConnectionMultiplexer
instance from StackExchange.Redis
. The RedisConnection
class recreates the connection when a connection is lost and unable to reconnect automatically.
For more information, see StackExchange.Redis and the code in a GitHub repo.
The home page layout for this sample is stored in the _Layout.cshtml file. From this page, you start the actual cache testing by clicking the Azure Cache for Redis Test from this page.
-
Open Views\Shared\_Layout.cshtml.
-
You should see in
<div class="navbar-header">
:<a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="RedisCache">Azure Cache for Redis Test</a>
:::image type="content" source="media/cache-web-app-aspnet-core-howto/cache-welcome-page.png" alt-text="screenshot of welcome page":::
From the home page, you select Azure Cache for Redis Test to see the sample output.
-
In Solution Explorer, expand the Views folder, and then right-click the Home folder.
-
You should see this code in the RedisCache.cshtml file.
@{ ViewBag.Title = "Azure Cache for Redis Test"; } <h2>@ViewBag.Title.</h2> <h3>@ViewBag.Message</h3> <br /><br /> <table border="1" cellpadding="10"> <tr> <th>Command</th> <th>Result</th> </tr> <tr> <td>@ViewBag.command1</td> <td><pre>@ViewBag.command1Result</pre></td> </tr> <tr> <td>@ViewBag.command2</td> <td><pre>@ViewBag.command2Result</pre></td> </tr> <tr> <td>@ViewBag.command3</td> <td><pre>@ViewBag.command3Result</pre></td> </tr> <tr> <td>@ViewBag.command4</td> <td><pre>@ViewBag.command4Result</pre></td> </tr> <tr> <td>@ViewBag.command5</td> <td><pre>@ViewBag.command5Result</pre></td> </tr> </table>
-
Execute the following command in your command window to build the app:
dotnet build
-
Then run the app with the following command:
dotnet run
-
Browse to
https://localhost:5001
in your web browser. -
Select Azure Cache for Redis Test in the navigation bar of the web page to test cache access.
:::image type="content" source="./media/cache-web-app-aspnet-core-howto/cache-simple-test-complete-local.png" alt-text="Screenshot of simple test completed local":::
If you continue to use this quickstart, you can keep the resources you created and reuse them.
Otherwise, if you're finished with the quickstart sample application, you can delete the Azure resources that you created in this quickstart to avoid charges.
Important
Deleting a resource group is irreversible. When you delete a resource group, all the resources in it are permanently deleted. Make sure that you do not accidentally delete the wrong resource group or resources. If you created the resources for hosting this sample inside an existing resource group that contains resources you want to keep, you can delete each resource individually on the left instead of deleting the resource group.
-
Sign in to the Azure portal, and then select Resource groups.
-
In the Filter by name... box, type the name of your resource group. The instructions for this article used a resource group named TestResources. On your resource group, in the results list, select ..., and then select Delete resource group.
:::image type="content" source="media/cache-web-app-howto/cache-delete-resource-group.png" alt-text="Delete":::
-
You're asked to confirm the deletion of the resource group. Type the name of your resource group to confirm, and then select Delete.
After a few moments, the resource group and all of its resources are deleted.