Creating a .NET MVC Project from Scratch Using Dotnet CLI
.NET MVC Web Development

Creating a .NET MVC Project from Scratch Using Dotnet CLI

Mishel Shaji
Mishel Shaji

When developing web applications with .NET, Visual Studio is a popular choice, but using the Command Line Interface (CLI) paired with Visual Studio Code (VS Code) gives you a lightweight, fast, and cross-platform workflow.

In this tutorial, you will learn how to create, build, and run a Model-View-Controller (MVC) web application using the .NET CLI and open it in VS Code.

If you like to use Visual Studio instead, please check the following article.

Creating a .NET MVC Project using Visual Studio
In this tutorial, we will walk through the professional approach to setting up an ASP.NET Core Model-View-Controller (MVC) application in Visual Studio using .NET 10. Instead of rushing into a single project setup, we will start with a clean approach by creating a Blank Solution first.

Understanding the dotnet new Command

The core tool for creating anything in the .NET ecosystem via the command line is the dotnet new command.

Think of dotnet new as a master craftsman with a box of blueprints. Instead of forcing you to write every file, folder, and configuration setting from scratch, you tell dotnet new which blueprint (called a template) you want to use. It then automatically scaffolds the entire folder structure and foundational code files for you.

To see a list of all available templates on your machine, you can always run:

dotnet new list

Step-by-Step Tutorial: Creating Your MVC App

Let's walk through creating a brand-new Model-View-Controller web application using .NET 10.

Step 1: Open Your Terminal or Command Prompt

Open your preferred terminal (Terminal on macOS/Linux, or PowerShell/Command Prompt on Windows) and navigate to the folder where you want to keep your project. For example:

Bash

cd Documents

Step 2: Run the Creation Command

To create an MVC project, we use the mvc template flag. We will also use the -n parameter, which stands for Name, to give our project and its root folder a specific title.

Run the following command:

Bash

dotnet new mvc -n MyBlogApp

What this command does:

  • dotnet new: Invokes the template engine.
  • mvc: Specifies that we want an ASP.NET Core Web App using the Model-View-Controller architecture.
  • -n MyBlogApp: Creates a new folder named MyBlogAppp and names the project file (.csproj) after it.

Step 3: Navigate into Your Project Folder

Before you can run or edit your code, you must move your terminal's focus inside the newly created directory:

cd MyBlogApp

Opening and Exploring the Project in VS Code

Now that your project files are generated, let’s open them in Visual Studio Code to see what dotnet new built for us.

In your terminal, type the following shortcut command to open the current folder directly in VS Code:

Bash

code .

(Note: If the code command doesn't work, simply open VS Code manually, click File > Open Folder, and choose your MyBlogApp directory).

The Generated Project Structure

Inside VS Code's file explorer, you will see a beginner-friendly layout categorized by responsibilities:

  • Controllers/: Contains the logic classes (like HomeController.cs) that handle incoming web browser requests.
  • Models/: Holds the data structures or business logic shapes.
  • Views/: Contains HTML files mixed with C# (called .cshtml files) that render what the user sees on their screen.
  • Program.cs: The main entry point of your application where the web server initializes and .NET 10 configurations are loaded.

Launching Your Application

To see your website live in action, you don't need to click any complex UI buttons. You can do it straight from the command line using the dotnet run command.

Go back to your terminal (or open the integrated terminal inside VS Code by pressing `Ctrl + ``) and execute:

Bash

dotnet run

What Happens Next:

The CLI will compile your C# files and spin up a lightweight, built-in development web server called Kestrel.

Your terminal output will look similar to this:

Building...
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: http://localhost:5000
info: Microsoft.Hosting.Lifetime[14]
      Now listening on: https://localhost:5001

Open any web browser (like Chrome, Edge, or Safari) and navigate to the address listed in your terminal (usually https://localhost:5001 or http://localhost:5000). You will see a fully functioning, styled template welcome homepage!

To stop the web server at any time, go back to your terminal window and press Ctrl + C.

Summary of Essential CLI Commands

Command What it does
dotnet new mvc -n Name Scaffolds a new MVC web application with a custom name.
code . Opens the current terminal directory inside VS Code.
dotnet build Compiles the project code to check for syntax errors without running it.
dotnet run Compiles and starts the local web server to view the app in a browser.

Practice Questions

1. What is the purpose of the -n parameter when running the dotnet new command?

  • Answer: The -n parameter stands for "Name". It specifies the name of the project file and automatically creates a folder with that exact name to house the project.

2. If you want to stop a running .NET web application in your terminal, what keyboard shortcut should you press?

  • Answer: You press Ctrl + C. This safely shuts down the Kestrel development web server.

3. Which file in your new .NET 10 project serves as the absolute starting point where your application configuration begins?

  • Answer: Program.cs. This file contains the foundational setup logic, routing configurations, and service registrations for the entire web application.

4. True or False: You must run dotnet build every single time right before you run dotnet run.

  • Answer: False. The dotnet run command automatically checks your code and builds/compiles it implicitly before launching the server.