Running Your First MVC Application

After understanding the ASP.NET Core MVC project structure, the next important step is learning how to run your first MVC application.

Running an MVC application allows you to see how all components such as controllers, views, and routing work together to generate a response in the browser.

This is a crucial step for beginners because it helps you understand how an ASP.NET Core application behaves in a real-world scenario.

In this guide, you will learn how to run your first MVC application, understand the request flow, and explore how the browser interacts with your application.


Prerequisites to Run an MVC Application

Before running your ASP.NET Core MVC application, make sure you have the required tools installed.

  • .NET SDK installed
  • Visual Studio or Visual Studio Code
  • Basic understanding of MVC structure

You can verify your .NET installation by running the following command:


dotnet --version

If the version number is displayed, your environment is ready.


Creating Your First ASP.NET Core MVC Application

You can create a new MVC application using the .NET CLI or Visual Studio.

Using the command line, run the following command:


dotnet new mvc -n FirstMvcApp

This command creates a new ASP.NET Core MVC project with the default structure.

After creating the project, navigate to the project folder:


cd FirstMvcApp

Running the MVC Application Using CLI

To run the application, use the following command:


dotnet run

Once the application starts, you will see output in the terminal showing the application URL.

It will look similar to this:


Now listening on: https://localhost:5001
Now listening on: http://localhost:5000

Open your browser and navigate to one of these URLs.

You will see the default ASP.NET Core MVC homepage.


Running MVC Application Using Visual Studio

In addition to using the .NET CLI, you can also run your ASP.NET Core MVC application using Visual Studio. This is the most common approach used by beginners and professional developers.

Visual Studio provides a graphical interface that makes it easy to build, run, and debug applications without using command-line commands.


Step 1: Open the Project in Visual Studio

Open Visual Studio and select Open a project or solution.

Navigate to your MVC project folder and open the .sln file.

This will load your entire ASP.NET Core MVC project inside Visual Studio.


Step 2: Set Startup Project

Make sure your MVC project is set as the startup project.

Right-click on the project in Solution Explorer and select:

  • Set as Startup Project

This ensures that Visual Studio runs the correct application.


Step 3: Run the Application

To run the application, click the Run button (green play button) at the top of Visual Studio.

You can run the application in two modes:

  • IIS Express
  • Kestrel (Project Name)

Both options will launch your application in a web browser.


Step 4: View Output in Browser

Once the application starts, Visual Studio will automatically open your default browser.

You will see the default ASP.NET Core MVC homepage.

The URL will look similar to:


https://localhost:5001

This confirms that your application is running successfully.


Step 5: Understanding Debug Mode

When you run the application using Visual Studio, it runs in debug mode by default.

This allows you to:

  • Set breakpoints in your code
  • Inspect variable values
  • Step through code execution
  • Identify and fix issues easily

For example, you can add a breakpoint inside a controller method:


public IActionResult Index()
{
    // Add breakpoint here
    return View();
}

When the application runs, execution will pause at the breakpoint.


Step 6: Stop the Application

To stop the running application, click the Stop button in Visual Studio.

This will terminate the application and free the port being used.


Why Use Visual Studio to Run MVC Applications?

Visual Studio provides several advantages when running ASP.NET Core MVC applications.

  • Easy debugging with breakpoints
  • Integrated build and run tools
  • Automatic browser launch
  • Better error visibility
  • Productivity tools for developers

For beginners, Visual Studio is the easiest way to start working with ASP.NET Core MVC applications.


Understanding What Happens When You Run the Application

When you run your MVC application, several important processes happen behind the scenes.

  • The application starts and configures services
  • The middleware pipeline is initialized
  • The routing system is activated
  • The browser sends an HTTP request
  • The request is handled by a controller
  • A view is returned as a response

This entire flow is handled automatically by ASP.NET Core.


Understanding the Default Route

ASP.NET Core MVC uses a default routing pattern to map incoming requests to controllers.

This configuration is defined in the Program.cs file.


app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}"
);

This means:

  • Controller = Home
  • Action = Index
  • Id = optional parameter

When you open the application, it automatically calls:

HomeController → Index action → Index view


Exploring the HomeController

The default MVC application contains a controller called HomeController.


public class HomeController : Controller
{
    public IActionResult Index()
    {
        return View();
    }
}

This controller handles incoming requests and returns the corresponding view.


Understanding the View

The view associated with the Index action is located in:

Views → Home → Index.cshtml

This file contains Razor syntax used to generate HTML content.

When the controller returns View(), this file is rendered in the browser.


Creating Your First Custom Page

Let’s create a simple custom page to understand how MVC works.

Add a new action in HomeController:


public IActionResult About()
{
    return View();
}

Now create a new view file:

Views → Home → About.cshtml


<h2>About Page</h2>
<p>This is my first MVC application.</p>

Now run the application and navigate to:

/Home/About

You will see your custom page.


How MVC Request Flow Works

When a user requests a page, the following steps occur:

  • The browser sends an HTTP request
  • The routing system identifies the controller and action
  • The controller processes the request
  • The controller returns a view
  • The view generates HTML
  • The response is sent back to the browser

This flow is the core of the MVC architecture.


Common Errors While Running MVC Applications

Beginners often face some common issues when running MVC applications.

  • Port already in use
  • Missing .NET SDK
  • Incorrect routing
  • View not found error

These issues can usually be resolved by checking configuration and project setup.


Tips for Beginners

  • Always check the terminal output for errors
  • Understand routing before creating pages
  • Keep controllers simple and focused
  • Follow MVC folder structure properly

Practicing small examples will help you understand the framework faster.


Summary

Running your first ASP.NET Core MVC application is an important step in understanding how web applications work.

You learned how to create, run, and explore an MVC application, as well as how routing, controllers, and views interact.

By practicing and experimenting with small changes, you can quickly build confidence and start developing real-world applications using ASP.NET Core MVC.