Add a controller

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.

Add MVC controller

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.

Last updated