ASP.NET Core Tutorial
Introduction to ASP.NET Core
ASP.NET Core is a free, open-source, and cross-platform framework for building modern, cloud-based, and internet-connected applications. With ASP.NET Core, you can build web apps and services, IoT apps, and mobile backends. It is a significant redesign of ASP.NET, and has been optimized for modern development workflows and tools.
Setting Up the Development Environment
To get started with ASP.NET Core, you need to set up your development environment. Follow the steps below:
- Install the .NET Core SDK from the official website.
- Install a code editor, such as Visual Studio Code or Visual Studio.
- Verify the installation by opening a terminal or command prompt and running the following command:
You should see the installed version of the .NET SDK.
Creating a New ASP.NET Core Project
To create a new ASP.NET Core project, use the following command in your terminal or command prompt:
This command creates a new web application project in a folder named MyWebApp. Navigate to the project directory:
Running the Application
To run the application, use the following command:
Open your web browser and navigate to https://localhost:5001 to see your running application.
Project Structure
The ASP.NET Core project structure includes several files and folders:
- Program.cs: Contains the entry point for the application.
- Startup.cs: Configures services and the app's request pipeline.
- appsettings.json: Contains configuration settings.
- wwwroot: Contains static files.
- Pages: Contains Razor pages for the application.
Adding a New Page
To add a new page to your application, follow these steps:
- Create a new Razor page in the Pages folder.
- Name the file About.cshtml.
- Add the following content to the file:
<h2>About</h2> <p>This is the about page.</p>
Navigate to https://localhost:5001/About to see the new page.
Adding a Controller
To add a new controller to your application, follow these steps:
- Create a new folder named Controllers.
- Create a new file named HomeController.cs in the Controllers folder.
- Add the following content to the file:
using Microsoft.AspNetCore.Mvc; namespace MyWebApp.Controllers { public class HomeController : Controller { public IActionResult Index() { return View(); } } }
Create a new folder named Views/Home and add a new file named Index.cshtml. Add the following content to the Index.cshtml file:
<h2>Home</h2> <p>Welcome to the home page.</p>
Navigate to https://localhost:5001/Home to see the new controller and view.
Conclusion
In this tutorial, you learned the basics of ASP.NET Core, including setting up the development environment, creating a new project, understanding the project structure, adding new pages, and creating controllers. ASP.NET Core is a powerful framework for building modern web applications, and this tutorial provides a foundation for further learning.