Create a controller

There are already a few controllers in the project’s Controllers folder, including the HomeController that renders the default welcome screen you see when you visit http://localhost:5000. You can ignore these controllers for now.

Create a new controller for the to-do list functionality, called TodoController, and add the following code:

Controllers/TodoController.cs

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Microsoft.AspNetCore.Mvc;
  6. namespace AspNetCoreTodo.Controllers
  7. {
  8. public class TodoController : Controller
  9. {
  10. // Actions go here
  11. }
  12. }

Routes that are handled by controllers are called actions, and are represented by methods in the controller class. For example, the HomeController includes three action methods (Index, About, and Contact) which are mapped by ASP.NET Core to these route URLs:

  1. localhost:5000/Home -> Index()
  2. localhost:5000/Home/About -> About()
  3. localhost:5000/Home/Contact -> Contact()

There are a number of conventions (common patterns) used by ASP.NET Core, such as the pattern that FooController becomes /Foo, and the Index action name can be left out of the URL. You can customize this behavior if you’d like, but for now, we’ll stick to the default conventions.

Add a new action called Index to the TodoController, replacing the // Actions go here comment:

  1. public class TodoController : Controller
  2. {
  3. public IActionResult Index()
  4. {
  5. // Get to-do items from database
  6. // Put items into a model
  7. // Render view using the model
  8. }
  9. }

Action methods can return views, JSON data, or HTTP status codes like 200 OK or 404 Not Found. The IActionResult return type gives you the flexibility to return any of these from the action.

It’s a best practice to keep controllers as lightweight as possible. In this case, the controller should only be responsible for getting the to-do items from the database, putting those items into a model the view can understand, and sending the view back to the user’s browser.

Before you can write the rest of the controller code, you need to create a model and a view.