> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Building the sample ASPNET Core Web App

> Guide to create, run, and containerize a simple ASP.NET Core Razor Pages app for local Kubernetes testing and deployment to AKS.

This guide shows how to create, run, and containerize a simple ASP.NET Core Razor Pages app so you can test it locally (on a single-node Kubernetes cluster via Docker Desktop) before deploying to AKS.

Prerequisites

* Windows 11 (used here), macOS, or Linux — steps are the same across OSes.
* Docker Desktop with Kubernetes enabled — download from [Docker Desktop](https://www.docker.com/products/docker-desktop).
* .NET 6 SDK — download from [https://dotnet.microsoft.com/en-us/download/dotnet/6.0](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
* An IDE or editor such as JetBrains Rider, Visual Studio, or VS Code.

<Callout icon="lightbulb" color="#1CB2FE">
  For local Kubernetes testing, enable Kubernetes in Docker Desktop Settings. That boots a single-node cluster suitable for development and learning.
</Callout>

<Callout icon="warning" color="#FF6B6B">
  Docker Desktop has licensing terms. Using it for local learning and development is typically allowed, but review the license if you plan production or commercial use.
</Callout>

Verify .NET SDK
Run this to confirm .NET 6 is installed:

```bash theme={null}
dotnet --list-sdks
# Example output:
# 6.0.405 [C:\Program Files\dotnet\sdk]
```

Create the ASP.NET Core app

1. Open your IDE.
2. Create a new solution using the "ASP.NET Core Web Application" template.
3. Give the solution a name (for example, `KodeKloudApp`) and choose the option to put the solution and project in the same directory so the solution contains a single project.
4. When creating the project, let the IDE generate a Dockerfile. We'll use Linux containers for the runtime.

<Frame>
  <img src="https://mintcdn.com/kodekloud-c4ac6d9a/DOaGc1j6cAGsXTOS/images/Azure-Kubernetes-Service/Building-and-containerizing-sample-application/Building-the-sample-ASPNET-Core-Web-App/rider-new-solution-aspnetcore-same-dir.jpg?fit=max&auto=format&n=DOaGc1j6cAGsXTOS&q=85&s=9ce7f1a0864052760bec1932574dcc8c" alt="A Windows desktop screenshot showing JetBrains Rider's &#x22;New Solution&#x22; dialog configured for an ASP.NET Core Web Application, with the &#x22;Put solution and project in the same directory&#x22; option highlighted. Desktop icons for Recycle Bin, Docker Desktop and JetBrains Rider are visible on the left." width="1920" height="1080" data-path="images/Azure-Kubernetes-Service/Building-and-containerizing-sample-application/Building-the-sample-ASPNET-Core-Web-App/rider-new-solution-aspnetcore-same-dir.jpg" />
</Frame>

Modify the app to read configuration
We’ll modify the Index page model to read a `Message` value from configuration sources (appsettings.json or environment variables). This keeps configuration flexible when running locally or in containers.

Update Pages/Index.cshtml.cs:

```csharp theme={null}
// Pages/Index.cshtml.cs
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;

namespace KodeKloudApp.Pages
{
    public class IndexModel : PageModel
    {
        private readonly ILogger<IndexModel> _logger;
        private readonly IConfiguration _configuration;

        public string Message { get; set; }

        public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public void OnGet()
        {
            Message = _configuration["Message"] ?? "Hello World";
        }
    }
}
```

Add the `Message` key to appsettings.json
A minimal `appsettings.json` with the `Message` key:

```json theme={null}
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Message": "Hello World"
}
```

Update the Index Razor page to render the message
Edit Pages/Index.cshtml to display `@Model.Message`:

```razor theme={null}
@page
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
    <p>@Model.Message</p>
</div>
```

Run and verify locally

* Start the app from your IDE (or `dotnet run` from the project folder).
* The page should show the `Message` from `appsettings.json`.
* Change the value in `appsettings.json` (for example, to `"Hello World changed"`) and restart the app to see the new text.

Example updated appsettings:

```json theme={null}
{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Message": "Hello World changed"
}
```

Building a Docker image (multi-stage Dockerfile)
The IDE-generated Dockerfile typically uses a multi-stage build:

* Build stage uses the .NET SDK to compile and publish.
* Final stage uses a smaller ASP.NET runtime image to reduce final image size.

Example multi-stage Dockerfile:

```dockerfile theme={null}
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["KodeKloudApp.csproj", "./"]
RUN dotnet restore "KodeKloudApp.csproj"
COPY . .
WORKDIR "/src/"
RUN dotnet build "KodeKloudApp.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "KodeKloudApp.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "KodeKloudApp.dll"]
```

Build the Docker image
From the project root (where the Dockerfile is present):

```bash theme={null}
docker build -t kodekloudapp:v1 .
```

Example build output (truncated):

```bash theme={null}
[+] Building 17.9s (7/17)
 => [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:...  16.1s
 ...
 => naming to docker.io/library/kodekloudapp:v1
```

Verify the image exists locally:

```bash theme={null}
docker images
# Example output includes:
# REPOSITORY        TAG     IMAGE ID       CREATED         SIZE
# kodekloudapp      v1      8177c6374c5c   2 minutes ago   200MB
```

If you prefer not to build locally, a pre-built image used in this lesson may be available on [Docker Hub](https://hub.docker.com/) so you can follow the Kubernetes deployment steps without rebuilding.

Quick reference: changed files and commands

| Item | Purpose | Example / Command |
| - | - | - |
| Project file | Main project | `KodeKloudApp.csproj` |
| Page model | Read configuration | `Pages/Index.cshtml.cs` (see code above) |
| Razor page | Display message | `Pages/Index.cshtml` (see code above) |
| Config | Default message value | `appsettings.json` |
| Build image | Create container image | `docker build -t kodekloudapp:v1 .` |
| Verify SDK | Check .NET SDK | `dotnet --list-sdks` |

Links and references

* Docker Desktop: [https://www.docker.com/products/docker-desktop](https://www.docker.com/products/docker-desktop)
* .NET 6 SDK downloads: [https://dotnet.microsoft.com/en-us/download/dotnet/6.0](https://dotnet.microsoft.com/en-us/download/dotnet/6.0)
* JetBrains Rider: [https://www.jetbrains.com/rider/](https://www.jetbrains.com/rider/)
* Learn about AKS: [https://learn.kodekloud.com/user/courses/azure-kubernetes-service](https://learn.kodekloud.com/user/courses/azure-kubernetes-service)
* ASP.NET Core documentation: [https://docs.microsoft.com/aspnet/core](https://docs.microsoft.com/aspnet/core)

This app is now ready to be deployed to your local Kubernetes cluster or pushed to a container registry for AKS or other environments.

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/azure-kubernetes-service/module/20789863-851c-44c5-a251-8cb7f78f60b5/lesson/aaa7c969-7244-422d-bd3f-b3d32a4b72ce" />
</CardGroup>


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.