|
| 1 | +--- |
| 2 | +title: Get started with ASP.NET Core |
| 3 | +author: tdykstra |
| 4 | +description: A short tutorial using the .NET CLI to create and run a basic Hello World app using ASP.NET Core Blazor. |
| 5 | +monikerRange: ">= aspnetcore-3.1" |
| 6 | +ms.author: tdykstra |
| 7 | +ms.custom: mvc |
| 8 | +ms.date: 07/23/2025 |
| 9 | +uid: get-started |
| 10 | +--- |
| 11 | +# Get started with ASP.NET Core |
| 12 | + |
| 13 | +[!INCLUDE[](~/includes/not-latest-version.md)] |
| 14 | + |
| 15 | +:::moniker range=">= aspnetcore-8.0" |
| 16 | + |
| 17 | +This tutorial shows how to create, run, and modify an ASP.NET Core Blazor Web App using the .NET CLI. *Blazor* is a .NET frontend web framework that supports both server-side rendering and client interactivity in a single programming model. |
| 18 | + |
| 19 | +You'll learn how to: |
| 20 | + |
| 21 | +> [!div class="checklist"] |
| 22 | +> * Create a Blazor Web App. |
| 23 | +> * Run the app. |
| 24 | +> * Change the app. |
| 25 | +> * Shut the app down. |
| 26 | +
|
| 27 | +## Prerequisites |
| 28 | + |
| 29 | +Obtain and install the latest .NET SDK at [Download .NET](https://dotnet.microsoft.com/download/dotnet). |
| 30 | + |
| 31 | +## Create a Blazor Web App |
| 32 | + |
| 33 | +Open a command shell to a suitable location for the sample app and use the following command to create a Blazor Web App. The `-o|--output` option creates a folder for the project and names the project `BlazorSample`: |
| 34 | + |
| 35 | +```dotnetcli |
| 36 | +dotnet new blazor -o BlazorSample |
| 37 | +``` |
| 38 | + |
| 39 | +## Run the app |
| 40 | + |
| 41 | +Change the directory to the `BlazorSample` folder with the following command: |
| 42 | + |
| 43 | +```dotnetcli |
| 44 | +cd BlazorSample |
| 45 | +``` |
| 46 | + |
| 47 | +The `dotnet watch` command runs the app and opens your default browser to the app's landing page: |
| 48 | + |
| 49 | +```dotnetcli |
| 50 | +dotnet watch |
| 51 | +``` |
| 52 | + |
| 53 | +:::image source="get-started/static/blazor-web-app-running.png" alt-text="Blazor Web App running in Microsoft Edge with the homepage rendered in the UI."::: |
| 54 | + |
| 55 | +Using the app's sidebar navigation, visit the Counter page, where you can select the **:::no-loc text="Click me":::** button to increment the counter. |
| 56 | + |
| 57 | +:::image source="get-started/static/blazor-web-app-counter-page-incremented-to-one.png" alt-text="Counter page rendered after the 'Click me' button is selected once, showing the counter incremented to a value of one."::: |
| 58 | + |
| 59 | +## Change the app |
| 60 | + |
| 61 | +Leave the browser open with the Counter page loaded. By using the `dotnet watch` command to run the app, you can make changes to the app's markup and code without having to rebuild the app to reflect the changes in the browser. |
| 62 | + |
| 63 | +The `Counter` Razor component that renders the Counter web page is located at `Components/Pages/Counter.razor` in the project. *Razor* is a syntax for combining HTML markup with C# code designed for developer productivity. |
| 64 | + |
| 65 | +Open the `Counter.razor` file in a text editor and note a few interesting lines that render content and make the component's counter feature work. |
| 66 | + |
| 67 | +`Components/Pages/Counter.razor`: |
| 68 | + |
| 69 | +```razor |
| 70 | +@page "/counter" |
| 71 | +
|
| 72 | +<PageTitle>Counter</PageTitle> |
| 73 | +
|
| 74 | +<h1>Counter</h1> |
| 75 | +
|
| 76 | +<p role="status">Current count: @currentCount</p> |
| 77 | +
|
| 78 | +<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> |
| 79 | +
|
| 80 | +@code { |
| 81 | + private int currentCount = 0; |
| 82 | +
|
| 83 | + private void IncrementCount() |
| 84 | + { |
| 85 | + currentCount++; |
| 86 | + } |
| 87 | +} |
| 88 | +``` |
| 89 | + |
| 90 | +The file starts with a line that indicates the component's relative path (`/counter`): |
| 91 | + |
| 92 | +```razor |
| 93 | +@page "/counter" |
| 94 | +``` |
| 95 | + |
| 96 | +The title of the page is set by `<PageTitle>` tags: |
| 97 | + |
| 98 | +```razor |
| 99 | +<PageTitle>Counter</PageTitle> |
| 100 | +``` |
| 101 | + |
| 102 | +An H1 heading is displayed: |
| 103 | + |
| 104 | +```razor |
| 105 | +<h1>Counter</h1> |
| 106 | +``` |
| 107 | + |
| 108 | +A paragraph element (`<p>`) displays the current count, which is stored in a variable named `currentCount`: |
| 109 | + |
| 110 | +```razor |
| 111 | +<p role="status">Current count: @currentCount</p> |
| 112 | +``` |
| 113 | + |
| 114 | +A button (`<button>`) allows the user to increment the counter, which occurs when a button click executes a C# method named `IncrementCount`: |
| 115 | + |
| 116 | +```razor |
| 117 | +<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> |
| 118 | +``` |
| 119 | + |
| 120 | +The `@code` block contains C# code that the component executes: |
| 121 | + |
| 122 | +* The counter variable `currentCount` is established with an initial value of zero. |
| 123 | +* The `IncrementCount` method is defined. The code within the method increments the `currentCount` variable by one each time the method is invoked. |
| 124 | + |
| 125 | +```csharp |
| 126 | +private int currentCount = 0; |
| 127 | + |
| 128 | +private void IncrementCount() |
| 129 | +{ |
| 130 | + currentCount++; |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +Let's change the increment of the counter in the `IncrementCount` method. |
| 135 | + |
| 136 | +Change the line so that `currentCount` is incremented by a value of ten each time `IncrementCount` is called: |
| 137 | + |
| 138 | +```diff |
| 139 | +- currentCount++; |
| 140 | ++ currentCount += 10; |
| 141 | +``` |
| 142 | + |
| 143 | +Save the file. |
| 144 | + |
| 145 | +As soon as you save the file, the running app is updated automatically because you used the `dotnet watch` command. Go back to the app in the browser and select the **:::no-loc text="Click me":::** button in the Counter page. Witness how the counter now increments from its existing value of one to a value of eleven. Each time the button is selected the value increments by ten. |
| 146 | + |
| 147 | +:::image source="get-started/static/blazor-web-app-counter-page-incremented-to-eleven.png" alt-text="Counter page rendered after the 'Click me' button is selected once, showing the counter incremented to a value of eleven."::: |
| 148 | + |
| 149 | +## Shut the app down |
| 150 | + |
| 151 | +Follow these steps: |
| 152 | + |
| 153 | +* Close the browser window. |
| 154 | +* To shut down the app, press <kbd>Ctrl</kbd>+<kbd>C</kbd> in the command shell. |
| 155 | + |
| 156 | +*Congratulations!* You've successfully completed this tutorial. |
| 157 | + |
| 158 | +## Next steps |
| 159 | + |
| 160 | +In this tutorial, you learned how to: |
| 161 | + |
| 162 | +> [!div class="checklist"] |
| 163 | +> * Create a Blazor Web App. |
| 164 | +> * Run the app. |
| 165 | +> * Change the app. |
| 166 | +> * Shut the app down. |
| 167 | +
|
| 168 | +:::moniker-end |
| 169 | + |
| 170 | +:::moniker range="< aspnetcore-8.0" |
| 171 | + |
| 172 | +This tutorial shows how to create and run an ASP.NET Core web app using the .NET CLI. |
| 173 | + |
| 174 | +For Blazor tutorials, see <xref:blazor/tutorials/index>. |
| 175 | + |
| 176 | +You'll learn how to: |
| 177 | + |
| 178 | +> [!div class="checklist"] |
| 179 | +> * Create a Razor Pages app. |
| 180 | +> * Run the app. |
| 181 | +> * Change the app. |
| 182 | +> * Shut the app down. |
| 183 | +
|
| 184 | +## Prerequisites |
| 185 | + |
| 186 | +Obtain and install the latest .NET SDK at [Download .NET](https://dotnet.microsoft.com/download/dotnet). |
| 187 | + |
| 188 | +## Create Razor Pages app |
| 189 | + |
| 190 | +Open a command shell to a suitable location for the sample app and use the following command to create a Razor Pages app. The `-o|--output` option creates a folder for the project and names the project `RazorPagesSample`: |
| 191 | + |
| 192 | +```dotnetcli |
| 193 | +dotnet new webapp -o RazorPagesSample |
| 194 | +``` |
| 195 | + |
| 196 | +## Run the app |
| 197 | + |
| 198 | +Change the directory to the `RazorPagesSample` folder with the following command: |
| 199 | + |
| 200 | +```dotnetcli |
| 201 | +cd RazorPagesSample |
| 202 | +``` |
| 203 | + |
| 204 | +The `dotnet watch` command runs the app and opens your default browser to the app's landing page: |
| 205 | + |
| 206 | +```dotnetcli |
| 207 | +dotnet watch |
| 208 | +``` |
| 209 | + |
| 210 | +:::image source="get-started/static/razor-pages-app-running.png" alt-text="Web app home page"::: |
| 211 | + |
| 212 | +## Change the app |
| 213 | + |
| 214 | +Open the `Pages/Index.cshtml` file in a text editor. |
| 215 | + |
| 216 | +After the line with the ":::no-loc text="Welcome":::" greeting, add the following line to display the local system date and time: |
| 217 | + |
| 218 | +```cshtml |
| 219 | +<p>The time on the server is @DateTime.Now</p> |
| 220 | +``` |
| 221 | + |
| 222 | +Save the changes. |
| 223 | + |
| 224 | +As soon as you save the file, the running app is updated automatically because you used the `dotnet watch` command. |
| 225 | + |
| 226 | +Refresh the page in the browser to see the result: |
| 227 | + |
| 228 | +:::image source="get-started/static/razor-pages-app-date-time-display.png" alt-text="Web app home page showing the change that was made."::: |
| 229 | + |
| 230 | +## Shut the app down |
| 231 | + |
| 232 | +To shut down the app: |
| 233 | + |
| 234 | +* Close the browser window. |
| 235 | +* Press <kbd>Ctrl</kbd>+<kbd>C</kbd> in the command shell. |
| 236 | + |
| 237 | +*Congratulations!* You've successfully completed this tutorial. |
| 238 | + |
| 239 | +## Next steps |
| 240 | + |
| 241 | +In this tutorial, you learned how to: |
| 242 | + |
| 243 | +> [!div class="checklist"] |
| 244 | +> * Create a Razor Pages app. |
| 245 | +> * Run the app. |
| 246 | +> * Change the app. |
| 247 | +> * Shut the app down. |
| 248 | +
|
| 249 | +:::moniker-end |
| 250 | + |
| 251 | +To learn more about ASP.NET Core, see the following: |
| 252 | + |
| 253 | +> [!div class="nextstepaction"] |
| 254 | +> <xref:index> |
0 commit comments