|
| 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/21/2025 |
| 9 | +uid: get-started |
| 10 | +--- |
| 11 | +# Get started with ASP.NET Core |
| 12 | + |
| 13 | +[!INCLUDE[](~/includes/not-latest-version.md)] |
| 14 | + |
| 15 | +This tutorial shows how to create, run, and modify an ASP.NET Core Blazor Web App using the .NET CLI. |
| 16 | + |
| 17 | +You'll learn how to: |
| 18 | + |
| 19 | +> [!div class="checklist"] |
| 20 | +> * Create a Blazor Web App. |
| 21 | +> * Run the app. |
| 22 | +> * Change the app. |
| 23 | +> * Shut the app down. |
| 24 | +
|
| 25 | +At the end, you'll have a working web app running on your local machine. |
| 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 web app project |
| 32 | + |
| 33 | +Open a command shell to a suitable location for the sample app and enter 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 | +Execute the following command to run the app: |
| 48 | + |
| 49 | +```dotnetcli |
| 50 | +dotnet watch |
| 51 | +``` |
| 52 | + |
| 53 | +The `dotnet watch` command runs the app and opens your default browser to the app's landing page. |
| 54 | + |
| 55 | +The browser shows the app's home page. |
| 56 | + |
| 57 | +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. Visit the Weather page to see a page load weather data. |
| 58 | + |
| 59 | +## Edit a Razor component |
| 60 | + |
| 61 | +Leave the browser open with the app loaded. By using the `dotnet watch` command to run the app, you can make changes to the app's markup and code and see the changes immediately reflected 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. |
| 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 | +The component starts with a line that indicates the component's relative path (`/counter`): |
| 68 | + |
| 69 | +```razor |
| 70 | +@page "/counter" |
| 71 | +``` |
| 72 | + |
| 73 | +The title of the page is set by `<PageTitle>` tags: |
| 74 | + |
| 75 | +```razor |
| 76 | +<PageTitle>Counter</PageTitle> |
| 77 | +``` |
| 78 | + |
| 79 | +An H1 heading is displayed: |
| 80 | + |
| 81 | +```razor |
| 82 | +<h1>Counter</h1> |
| 83 | +``` |
| 84 | + |
| 85 | +A paragraph element (`<p>`) displays the current count, which is stored in a variable named `currentCount`: |
| 86 | + |
| 87 | +```razor |
| 88 | +<p role="status">Current count: @currentCount</p> |
| 89 | +``` |
| 90 | + |
| 91 | +A button (`<button>`) allows the user to call a C# method named `IncrementCount` when the button is selected: |
| 92 | + |
| 93 | +```razor |
| 94 | +<button class="btn btn-primary" @onclick="IncrementCount">Click me</button> |
| 95 | +``` |
| 96 | + |
| 97 | +C# code is present in the `@code` block: |
| 98 | + |
| 99 | +```razor |
| 100 | +@code { |
| 101 | + ... |
| 102 | +} |
| 103 | +``` |
| 104 | + |
| 105 | +Within the `@code` block: |
| 106 | + |
| 107 | +* The counter variable `currentCount` is established with an initial value of zero. |
| 108 | +* The `IncrementCount` method is defined. The code within the method increments the `currentCount` variable by one each time the method is invoked. |
| 109 | + |
| 110 | +```csharp |
| 111 | +private int currentCount = 0; |
| 112 | + |
| 113 | +private void IncrementCount() |
| 114 | +{ |
| 115 | + currentCount++; |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +Let's change the increment of the counter in the `IncrementCount` method. |
| 120 | + |
| 121 | +Change the line so that `currentCount` is incremented by a value of ten each time `IncrementCount` is called: |
| 122 | + |
| 123 | +```diff |
| 124 | +- currentCount++; |
| 125 | ++ currentCount += 10; |
| 126 | +``` |
| 127 | + |
| 128 | +Save the file. |
| 129 | + |
| 130 | +As soon as you save the file, the running app is updated automatically because you used the `dotnet watch` command to run the app. Go back to the app in the browser with the Counter page loaded and select the **:::no-loc text="Click me":::** button. Witness how the counter now increments by ten. |
| 131 | + |
| 132 | +To shut down the app: |
| 133 | + |
| 134 | +* Close the browser window. |
| 135 | +* Press <kbd>Ctrl</kbd>+<kbd>C</kbd> in the command shell. |
| 136 | + |
| 137 | +*Congratulations!* You've successfully completed this tutorial. |
| 138 | + |
| 139 | +## Next steps |
| 140 | + |
| 141 | +In this tutorial, you learned how to: |
| 142 | + |
| 143 | +> [!div class="checklist"] |
| 144 | +> * Create a Blazor Web App. |
| 145 | +> * Run the app. |
| 146 | +> * Change the app. |
| 147 | +> * Shut the app down. |
| 148 | +
|
| 149 | +To learn more about ASP.NET Core, see the following: |
| 150 | + |
| 151 | +> [!div class="nextstepaction"] |
| 152 | +> <xref:index#recommended-learning-path> |
| 153 | +
|
| 154 | +## Additional resources |
| 155 | + |
| 156 | +[Additional Blazor tutorials](xref:blazor/tutorials/index) |
0 commit comments