Getting Started with ASP.NET Core
Introduction to ASP.NET Core
ASP.NET Core is a free, cross-platform, open-source framework for building modern, cloud-enabled, internet-connected applications. You can build web applications, IoT applications, and mobile backends using ASP.NET Core.
Key features of ASP.NET Core include:
- Unified Model: ASP.NET Core integrates the MVC and Web API frameworks into a single programming model.
- Cross-Platform: Develop and run your applications on Windows, macOS, and Linux.
- Open Source: The framework is developed in the open on GitHub.
- Performance: ASP.NET Core is designed for high performance, with improvements in request processing, middleware pipeline, and memory management.
- Dependency Injection: Built-in support for dependency injection simplifies application design and testing.
- Modern Tooling: Works seamlessly with Visual Studio, VS Code, and the .NET CLI.
Prerequisites
Before you begin, ensure you have the following installed:
- .NET SDK: Download and install the latest .NET SDK from the official .NET website (https://dotnet.microsoft.com/download).
- Code Editor: A code editor like Visual Studio Code, Visual Studio, or JetBrains Rider. Visual Studio Code is recommended for its lightweight nature and extensive extension ecosystem.
Note
Ensure the .NET SDK is added to your system's PATH environment variable. You can verify this by opening a terminal or command prompt and typing dotnet --version.
Installation
ASP.NET Core development is primarily done by installing the .NET SDK. There is no separate ASP.NET Core installation required.
To verify your installation, open your terminal or command prompt and run:
dotnet --version
This command should output the version of the .NET SDK you have installed.
Create Your First ASP.NET Core Project
You can create a new ASP.NET Core project using the .NET CLI. We'll start with a simple Razor Pages project.
- Open your terminal or command prompt.
- Navigate to the directory where you want to create your project.
- Run the following command to create a new Razor Pages project named
MyFirstAspNetCoreApp:dotnet new razor -o MyFirstAspNetCoreApp - Change directory into the newly created project folder:
cd MyFirstAspNetCoreApp
Example Command
This command uses the dotnet new command with the razor template to create a new project directory named MyFirstAspNetCoreApp. The -o flag specifies the output directory.
Run Your Application
Once you have created your project, you can run it directly from the terminal.
- Make sure you are in your project directory (e.g.,
MyFirstAspNetCoreApp). - Run the following command:
dotnet run
The .NET CLI will compile and start your application. You will see output similar to this:
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
Now listening on: https://localhost:5001
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
Open your web browser and navigate to http://localhost:5000 or https://localhost:5001. You should see the default welcome page for your ASP.NET Core application.
Tip
Press Ctrl+C in the terminal to stop the running application.
Understanding the Project Structure
Let's briefly look at the key files and folders created in your new Razor Pages project:
Pages/: Contains your Razor Pages (.cshtml files) and their code-behind files (.cshtml.cs). Each .cshtml file represents a page in your application.Pages/_Layout.cshtml: The main layout file that defines the overall structure of your web pages.Pages/_ViewStart.cshtml: Specifies the default layout to be used for Razor Pages.Pages/Error.cshtml: A Razor Page to display error information.Program.cs: The entry point of your application. It configures the host and the application's request pipeline.Startup.cs: (In older .NET Core versions, now integrated intoProgram.csfor .NET 6 and later) Configures application services and the HTTP request pipeline.appsettings.json: Contains application configuration settings.MyFirstAspNetCoreApp.csproj: The project file that defines project dependencies and settings.
For example, the Pages/Index.cshtml file is typically the home page of your application.
MVC vs. Razor Pages
ASP.NET Core offers two primary patterns for building web UIs:
- Model-View-Controller (MVC): An architectural pattern that separates concerns into Model, View, and Controller. It's powerful for complex applications with clear separation of logic.
- Razor Pages: A page-centric model that simplifies building web UIs for scenarios where you don't need a full MVC architecture. It focuses on individual pages and their logic.
Razor Pages is often recommended for beginners and for simpler web applications as it reduces the boilerplate code required by MVC.
Next Steps
Congratulations on creating and running your first ASP.NET Core application!
Here are some recommended next steps:
- Explore the contents of the
Pages/directory and modifyIndex.cshtml. - Learn about routing and how to create new pages.
- Dive deeper into Razor syntax for dynamic content.
- Understand how to work with data, including Entity Framework Core.
- Explore ASP.NET Core MVC if you need a more robust architectural pattern.
Continue your learning journey with the comprehensive documentation available on Microsoft Docs.