# 15.0: API Controller

## Add an API Controller

1. Stop the app
2. Right click on **ElevenNote.WebApi -> Controllers** and click **Add -> Controller**
3. Select **Web Api 2 Controller - Empty**
4. Name it **NoteController**(or whatever your controller needs to be named if this is an alternative app)

   ![Controller](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAxpyeezdS3jPxJnKOs%2F-LAxq-rVOcCW4U_oSTOw%2F15.0-A.png?generation=1524677219461973\&alt=media)
5. Add an `[Authorize]` attribute tag at the top
6. Inside the controller, add a helper method that creates a `NoteService` similar to our method in the `ElevenNote.Web` MVC project

   ```csharp
    namespace ElevenNote.WebAPI.Controllers
    {
        [Authorize]
        public class NoteController : ApiController
        {
            private NoteService CreateNoteService()
            {
                var userId = Guid.Parse(User.Identity.GetUserId());
                var noteService = new NoteService(userId);
                return noteService;
            }
        }
    }
   ```
7. `CTRL .` to bring in the using statement for the `NoteService` project and Microsoft.Asp.Net.Identity
8. This will allow us to reuse our `NoteService` in the methods for the API project, much like we did in the MVC project
9. Above the service method, write a method that uses the Service:

   ```csharp
    namespace ElevenNote.WebAPI.Controllers
    {
        [Authorize]
        public class NoteController : ApiController
        {
            public IHttpActionResult Get()
            {
                NoteService noteService = CreateNoteService();
                var notes = noteService.GetNotes();
                return Ok(notes);
            }

            private NoteService CreateNoteService()
            {
                var userId = Guid.Parse(User.Identity.GetUserId());
   ```
10. `CTRL .` to bring in the using statement and then consider doing a  ![Git](https://3258533034-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LAU8YfMZK4G04fkoGGv%2F-LAwyv0-ttugakHO6zeV%2F-LAwyxUd0dcYSLXYb-5s%2Fdevicons_github_badge.png?generation=1524662787257488\&alt=media)

[Next,](https://eleven-fifty-academy.gitbook.io/dotnet-201-elevennote/elevennote-api-parts-13-19/part-15-api-controller/15.1-postmangetnotes) we'll test getting notes in Postman.
