创建控制器

在项目的 Controllers 目录里,已经预置了几个控制器,其中有渲染默认欢迎页的 HomeController,就是你访问 http://localhost:5000 看到的那个页面。暂时不用管这些控制器。

给待办清单功能创建一个新的控制器,取名叫 TodoController ,并添加如下代码:

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
  11. }
  12. }

由控制器本身处理的路由叫 action ,在控制器类里用方法表示。比如,HomeController 包含三个 action 方法(IndexAbout,和 Contact),由 ASP.NET Core 分别映射到如下的 URL:

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

ASP.NET Core 中有几个惯例(常见的模式),比如这个 FooController 映射到 /Foo 的模式,还有 Index 的 action名 可以在 URL 里省略。如果你有需要,可以自定义这些行为,不过就目前的情况,让我们暂且遵循这些惯例。

TodoController 里,添加一个名为 Index 的 action,把那句 // Actions go here 注释替换掉:

  1. public class TodoController : Controller
  2. {
  3. public IActionResult Index()
  4. {
  5. // 从数据库获取 to-do 条目
  6. // 把条目置于 model 中
  7. // 使用 model 渲染视图
  8. }
  9. }

一个 action 方法可以返回视图、JSON数据,或者 200 OK404 Not Found 之类的状态码。返回类型 IActionResult 给了你足够的灵活性,以返回上面提到的任意一个。

使控制器尽可能保持轻量化,是一个良好的习惯。在当前的情形里,这个控制器应该仅仅完成这些事情:从数据库取出待办事项的记录,把这些事项包装在一个可用于视图的模型中,并把这个视图发送到用户的浏览器。

继续编码这个控制器之前,你需要创建 模型 和 视图。


Create a controller

There are already a few controllers in the project’s Controllers directory, 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 and 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 will 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.