ASP Module 2
Getting Started with MVC
Getting Started with MVC
  • Overview
    • Introduction to MVC
  • Understanding MVC
    • Creating an MVC app
    • Add a controller
      • Understand HTTP endpoint
      • Understand basic routing
    • Add a view
      • Understanding shared layouts
      • Setting view title
      • Passing data to view
  • Exercise
    • Add two numbers
      • Using Model
  • Display Movies
    • Add a model
      • Scaffold the index view
      • Scaffold the movies controller
      • Add temp movie data
      • Pass data to index view
    • Implement actions
      • The first create action
      • The second create action
      • The details action method
      • Edit & Delete actions
      • Asynchronous actions
    • Repo link
Powered by GitBook
On this page
  1. Understanding MVC

Add a controller

PreviousCreating an MVC appNextUnderstand HTTP endpoint

Last updated 2 years ago

Adding Controller in Visual Studio

The MVC project contains folders for the Controllers and Views, follow the steps shown below

In Solution Explorer, right-click Controllers > Add > Controller.

Solution Explorer, right click Controllers > Add > Controller

In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.

In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs and select Add.

Replace the contents of Controllers/HelloWorldController.cs with the following code:

using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;

namespace MvcMovie.Controllers;

public class HelloWorldController : Controller
{
    // 
    // GET: /HelloWorld/
    public string Index()
    {
        return "This is my default action...";
    }
    // 
    // GET: /HelloWorld/Welcome/ 
    public string Welcome()
    {
        return "This is the Welcome action method...";
    }
}

Every public method in a controller is callable as an HTTP endpoint. Note the comments preceding each method.

Add MVC controller