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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace AspNetCoreTodo.Controllers
{
public class TodoController : Controller
{
// Actions go here
}
}
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:
localhost:5000/Home -> Index()
localhost:5000/Home/About -> About()
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:
public class TodoController : Controller
{
public IActionResult Index()
{
// Get to-do items from database
// Put items into a model
// Render view using the model
}
}
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.