Building web applications from the command line gives you speed and direct control over your workspace. If you want to spin up a project without opening a heavy integrated development environment, the Microsoft .NET CLI provides built-in scaffolding tools. You can generate a fully functional Model-View-Controller web application using a single command in your terminal.
The core command you need is dotnet new mvc. This instruction invokes the template engine inside the .NET SDK, building a complete directory structure with controllers, models, views, and configuration files ready for local execution.
Prerequisites for ASP.NET Core Development
Before running any terminal commands, you'll need the proper environment setup on your machine. Without the correct software packages installed, the template engine won't recognize the arguments.
Make sure you download and install the official .NET SDK matching your target development version. Once installed, verify your setup by opening your terminal—whether you use PowerShell, Command Prompt, or bash—and typing a quick version check:
dotnet --version
If the CLI responds with a valid version number like 8.0.x or 9.0.x, you're ready to proceed. If you encounter an error stating that the command isn't found, you'll need to restart your terminal or reinstall the SDK so that it adds the executable path to your system environment variables.
Executing the dotnet new mvc Command
Generating your project folder takes only a second. You can create the application right in your current directory or specify an output path using command flags.
Navigate to your workspace directory in your terminal, then execute the command with an output flag to define your project folder name:
dotnet new mvc -o MyMvcApp
Behind the scenes, this command creates a folder named MyMvcApp. Inside, it generates the project file (.csproj), program entry points, static assets, and the standard MVC folder tree.
Key Command Options
You don't have to settle for default settings. The template engine accepts several parameters to customize your initial setup:
- -o or --output: Specifies the destination directory for the generated files. If omitted, it builds everything in your active directory.
- -n or --name: Sets the internal assembly name and default namespace if you want it to differ from your folder name.
- -f or --framework: Targets a specific Target Framework Moniker, such as
net8.0ornet9.0, overriding your global SDK default. - -au or --auth: Configures authentication options right away. Passing
-au Nonebuilds an unauthenticated app, while other flags support individual user accounts.
For instance, if you want to name your project namespace differently from the directory, combine the flags like this:
dotnet new mvc -n CustomNamespace -o MyMvcApp
Exploring the Generated File Structure
Once the scaffolding process finishes, change your directory into the new project folder to inspect what the template built for you:
cd MyMvcApp
If you open this directory in a code editor like Visual Studio Code, you'll notice a clean, organized hierarchy designed around the MVC architectural pattern:
- Controllers/: Contains classes like
HomeController.csthat handle incoming browser requests, manipulate data models, and return responses or views. - Models/: Holds your business objects and data classes. By default, you'll find an
ErrorViewModel.csused for handling exceptions gracefully. - Views/: Contains Razor template files (
.cshtml). Subdirectories match your controller names, keeping UI components neatly separated. - Program.cs: The central entry point where dependency injection, routing, middleware pipelines, and application services are configured.
- appsettings.json: Manages configuration settings, including logging rules and database connection strings.
Building and Running Your Application
With your project scaffolded, you can compile and launch the web server straight from the terminal without touching a graphical IDE.
First, restore any necessary NuGet dependencies and build the source code to verify there are no compilation errors:
dotnet build
Next, start the local development server using the run command:
dotnet run
The terminal will output local hosting details, typically displaying URLs like http://localhost:5000 or https://localhost:5076. Copy one of these links, paste it into your web browser, and you'll see the default ASP.NET Core welcome page.
To shut down the local server when you're done testing, return to your terminal window and press Ctrl + C.
Common Mistakes to Avoid
Developers often run into a few recurring stumbling blocks when spinning up projects via the terminal. Keep these common pitfalls in mind:
- Running commands outside the SDK scope: If you installed a new SDK version but your terminal still points to an older global toolset, your template flags might fail. Always check your runtime version first.
- Ignoring HTTPS certificates: Local development on ASP.NET Core relies on local certificates. If your browser blocks the local localhost connection due to security warnings, you can trust the local development certificate by running
dotnet dev-certs https --trust. - Confusing output and name flags: Remember that
-odictates where files physically land on your disk, whereas-nalters the C# namespace inside your code files. Mismatching them can cause minor friction when importing namespaces later.
Frequently Asked Questions
Can I use a different programming language like F# with the MVC template?
Yes. While C# is the default language for ASP.NET Core, the template engine supports language flags. You can check template availability for alternative languages by referencing the language option documentation in the official .NET CLI guide.
How do I add a database context to this generated project later?
You can install Entity Framework Core packages using the package manager command line. Run dotnet add package Microsoft.EntityFrameworkCore.SqlServer inside your project directory to inject database connectivity tools directly into your .csproj file.
Do I need Visual Studio installed to run dotnet new mvc?
No. The .NET SDK operates completely independently. You can build, test, and run your MVC application using only the command-line interface and a lightweight code editor like Visual Studio Code on Windows, macOS, or Linux.
How do I update my MVC project templates?
Templates ship bundled directly with the .NET SDK installer. To get newer template versions or bug fixes, download and install the latest patch or major release of the .NET SDK from Microsoft.