API | Description | Request body | Response body |
---|---|---|---|
GET /api/todo | Get all to-do items | None | Array of to-do items |
GET /api/todo/{id} | Get an item by ID | None | To-do item |
POST /api/todo | Add a new item | To-do item | To-do item |
PUT /api/todo/{id} | Update an existing item | To-do item | None |
DELETE /api/todo/{id} | Delete an item. | None | None |
TodoApi
and tap OK.TodoItem
class. Right-click the Models folder and select Add > New Item.TodoItem
and click OK.ITodoRepository
. Use the class template (Add New Item > Class).TodoRepository
class that implements ITodoRepository
:TodoRepository
inside the controller we will inject an ITodoRepository
the built-in support in ASP.NET Core for dependency injection.ITodoRepository
. That way, the test narrowly targets the controller logic and not the data access layer.ConfigureServices
method, add the highlighted code:TodoController
.TodoController
class.GET/api/todo
GET/api/todo/{id}
GetAll
method:[Route('api/[controller]')]
[HttpGet]
attribute also has a template string, append that to the path. This sample doesn’t use a template string.GetById
method, “{id}” is a placeholder variable. In the actual HTTP request, the client will use the ID of the todo
item. At runtime, when MVC invokes GetById
, it assigns the value of “{id}” in the URL the method’s id
parameter.GetAll
method returns a CLR object. 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.)GetById
method returns the more general IActionResult
type, which represents a generic result type. That’s because GetById
has two different return types:NotFound
.http://localhost:port/api/todo
, where port is a randomly chosen port number. If you’re using Chrome, Edge or Firefox, the todo data will be displayed. If you’re using IE, IE will prompt to you open or save the todo.json file.http://localhost:port/api/todo
, where port is the port number. Click Execute to send the request.Create
, Update
, and Delete
methods to the controller. These methods are variations on a theme, so I’ll just show the code and highlight the main differences.CreateAtRoute
also adds a Location header to the response. The Location header specifies the URI of the newly created to-do item. See 10.2.2 201 Created.Content-Type:application/json
, which is a Content-Type
header with the value application/json
. Fiddler automatically adds the Content-Length header.{'Name':'<yourto-doitem>'}
Update
is similar to Create
, but uses HTTP PUT. The response is 204 (No Content).According to the HTTP spec, a PUT request requires the client to send the entire updated entity, not just the deltas. To support partial updates, use HTTP PATCH.File -> New
you won't be able to work on UWP. If you are bringing over a project that includes projects like UWP that aren't exactly supported on Mac you should still be able to work on the code, you just cannot build or deploy.