Getting Started: Building Your First .NET Web Application
Welcome to the .NET ecosystem for web development! This guide will walk you through the essential steps to create your first web application using .NET.
Prerequisites
- .NET SDK: Ensure you have the latest .NET SDK installed. You can download it from the official .NET website.
- Code Editor: A code editor like Visual Studio Code, Visual Studio, or Rider is highly recommended.
- Terminal/Command Prompt: You'll use this to run .NET commands.
Step 1: Create a New Web Project
We'll use the .NET CLI (Command Line Interface) to scaffold a new web application. We'll create an ASP.NET Core MVC (Model-View-Controller) project, a popular pattern for building web apps.
Open Your Terminal
Navigate to the directory where you want to create your project.
cd path/to/your/projects
Create the Project
Run the following command to create a new MVC web application named "MyWebApp":
dotnet new mvc -o MyWebApp
This command creates a new directory named MyWebApp
and generates all the necessary files for an MVC project.
Step 2: Navigate and Run Your Application
Now, let's explore the project structure and run the application.
Navigate to the Project Directory
cd MyWebApp
Run the Application
Use the dotnet run
command to start your web server:
dotnet run
The output will indicate that the application is listening on a specific port (usually https://localhost:7XXX
or http://localhost:5XXX
). Open your web browser and navigate to that URL.
You should see the default ASP.NET Core welcome page!
Step 3: Explore the Project Structure
Inside the MyWebApp
directory, you'll find several key files and folders:
- Controllers: Contains classes that handle incoming HTTP requests and return responses (e.g.,
HomeController.cs
). - Views: Holds the HTML templates (Razor files,
.cshtml
) that define the user interface. - Models: Typically used to represent data and business logic.
- wwwroot: Contains static files like CSS, JavaScript, and images.
- Program.cs: The entry point of your application, where the web host is configured.
- Startup.cs (in older .NET versions) or configuration within
Program.cs
: Configures application services and the request pipeline.
Step 4: Make Your First Change (Example: Modify the Homepage)
Let's change the content of the homepage.
Edit the View
Open the file Views/Home/Index.cshtml
in your code editor.
Locate the section with the <h1>Hello, World!</h1>
tag and change it to something like:
<h1>Welcome to My Awesome .NET App!</h1>
Restart and Refresh
If your application is still running, stop it by pressing Ctrl+C
in the terminal. Then, run dotnet run
again.
Refresh your browser page. You should now see your updated homepage title!
Next Steps
Congratulations! You've built and run your first .NET web application. Here are some areas to explore next:
- Routing: Learn how URLs map to controller actions.
- Razor Pages: An alternative, simpler model for building page-focused web UI.
- Data Access: Integrate with databases using Entity Framework Core.
- APIs: Build RESTful APIs with ASP.NET Core.
- Deployment: Learn how to deploy your application to cloud services like Azure.