3.5 KiB
[!code-csharpMain]
The preceding code:
- Defines an empty controller class. In the next sections, we'll add methods to implement the API.
- The constructor uses Dependency Injection to inject the database context (
TodoContext
) into the controller. The database context is used in each of the CRUD methods in the controller. - The constructor adds an item to the in-memory database if one doesn't exist.
Getting to-do items
To get to-do items, add the following methods to the TodoController
class.
[!code-csharpMain]
These methods implement the two GET methods:
GET /api/todo
GET /api/todo/{id}
Here is an example HTTP response for the GetAll
method:
HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
Date: Thu, 18 Jun 2015 20:51:10 GMT
Content-Length: 82
[{"Key":"1", "Name":"Item1","IsComplete":false}]
Later in the tutorial I'll show how you can view the HTTP response using Postman or curl.
Routing and URL paths
The [HttpGet]
attribute specifies an HTTP GET method. The URL path for each method is constructed as follows:
- Take the template string in the controller’s route attribute:
[!code-csharpMain]
- Replace "[Controller]" with the name of the controller, which is the controller class name minus the "Controller" suffix. For this sample, the controller class name is TodoController and the root name is "todo". ASP.NET Core routing is not case sensitive.
- If the
[HttpGet]
attribute has a route template (such as[HttpGet("/products")]
, append that to the path. This sample doesn't use a template. See Attribute routing with Http[Verb] attributes for more information.
In the GetById
method:
[HttpGet("{id}", Name = "GetTodo")]
public IActionResult GetById(long id)
"{id}"
is a placeholder variable for the ID of the todo
item. When GetById
is invoked, it assigns the value of "{id}" in the URL to the method's id
parameter.
Name = "GetTodo"
creates a named route and allows you to link to this route in an HTTP Response. I'll explain it with an example later. See Routing to Controller Actions for detailed information.
Return values
The GetAll
method returns an IEnumerable
. MVC automatically serializes the object to JSON and writes the JSON into the body of the response message. The response code for this method is 200, assuming there are no unhandled exceptions. (Unhandled exceptions are translated into 5xx errors.)
In contrast, the GetById
method returns the more general IActionResult
type, which represents a wide range of return types. GetById
has two different return types:
-
If no item matches the requested ID, the method returns a 404 error. This is done by returning
NotFound
. -
Otherwise, the method returns 200 with a JSON response body. This is done by returning an
ObjectResult