LibraryCreating a Basic ASP.NET Core Web Application

Creating a Basic ASP.NET Core Web Application

Learn about Creating a Basic ASP.NET Core Web Application as part of C# .NET Development and Azure Integration

Creating Your First ASP.NET Core Web Application

Welcome to the exciting world of ASP.NET Core web development! This module will guide you through the fundamental steps of creating a basic web application using C# and .NET. We'll cover setting up your development environment, understanding the project structure, and running your first application.

Prerequisites and Setup

Before we dive in, ensure you have the necessary tools installed. This includes the .NET SDK and a code editor like Visual Studio or Visual Studio Code. These tools provide the environment and commands needed to build and run your ASP.NET Core applications.

What are the two essential tools needed to start developing ASP.NET Core applications?

The .NET SDK and a code editor (e.g., Visual Studio, Visual Studio Code).

Creating a New Project

You can create a new ASP.NET Core project using the .NET CLI (Command Line Interface) or your chosen IDE. The CLI command

code
dotnet new webapp
is a quick way to scaffold a new web application project with a Razor Pages structure, which is a common starting point for many web applications.

The `dotnet new webapp` command generates a foundational project structure.

This command creates a set of pre-configured files and folders, including a startup class, configuration files, and basic page templates, ready for customization.

When you execute dotnet new webapp -o MyWebApp, the .NET CLI creates a new directory named 'MyWebApp' and populates it with the essential files for a Razor Pages application. This includes a Program.cs file for application bootstrapping, appsettings.json for configuration, a Pages folder containing Razor Pages and their code-behind files, and a wwwroot folder for static assets like CSS and JavaScript.

Understanding the Project Structure

A typical ASP.NET Core web application project has a well-defined structure that helps organize your code. Key directories include

code
Pages
for Razor Pages,
code
wwwroot
for static files, and configuration files like
code
appsettings.json
.

Folder/FilePurpose
Pages/Contains Razor Pages (.cshtml files) and their associated code-behind files (.cshtml.cs).
wwwroot/Holds static assets such as HTML, CSS, JavaScript, and images.
Program.csThe entry point of the application, responsible for bootstrapping and configuring the web host.
appsettings.jsonStores application configuration settings, including connection strings and logging levels.
Startup.cs (older .NET versions) / Program.cs (newer .NET versions)Configures the application's request pipeline, services, and middleware.

Running Your Application

Once your project is created, you can run it using the .NET CLI command

code
dotnet run
from your project's root directory. This command builds and starts the web server, making your application accessible via a local URL, typically http://localhost:5000 or http://localhost:5001.

The Program.cs file in modern ASP.NET Core applications (using .NET 6 and later) consolidates the web host builder and application startup logic. It defines the application's request pipeline using WebApplication.CreateBuilder(args) and app.Run(). The builder configures services and the HTTP request pipeline. The app.Run() method starts the Kestrel web server, listening for incoming requests. Key middleware like routing, authentication, and static file serving are configured here.

📚

Text-based content

Library pages focus on text content

The default template often includes a simple homepage and a privacy policy page. You can view these by navigating to the respective URLs in your browser after running the application.

Next Steps: Adding Functionality

With your basic application running, you're ready to start adding features. This involves creating new Razor Pages, defining models to represent your data, and implementing logic in your C# code-behind files. Understanding how to handle HTTP requests and responses is crucial for building interactive web applications.

Learning Resources

Get started with ASP.NET Core(documentation)

The official Microsoft documentation provides a comprehensive guide to setting up your environment and creating your first ASP.NET Core application.

Create a web app with ASP.NET Core Razor Pages(tutorial)

A step-by-step tutorial demonstrating how to create a basic Razor Pages web application using Visual Studio Code.

ASP.NET Core Fundamentals(tutorial)

A Microsoft Learn module covering the core concepts and architecture of ASP.NET Core.

Introduction to ASP.NET Core(video)

A foundational video explaining what ASP.NET Core is and its key benefits.

ASP.NET Core Project Structure Explained(video)

This video breaks down the typical file and folder structure of an ASP.NET Core project.

ASP.NET Core .NET CLI Overview(documentation)

Learn how to use the .NET CLI commands to create, build, and manage your ASP.NET Core projects.

Razor Pages in ASP.NET Core(documentation)

Detailed documentation on Razor Pages, a page-focused model for building web UIs in ASP.NET Core.

ASP.NET Core Middleware(documentation)

Understand how middleware components process HTTP requests and responses in ASP.NET Core.

ASP.NET Core Configuration(documentation)

Learn how to manage application settings and configuration in ASP.NET Core applications.

What is ASP.NET Core?(wikipedia)

A general overview of ASP.NET Core, its history, and its place in web development.