14.0: WebAPI Setup
Last updated
Last updated
In this section, we will set up the WebAPI.
Right click on the solution and Add -> New Project
Choose ASP.NET Web Application.
Name it ElevenNote.WebApi, like the following screenshot:
In the menu(see below), select Web API.
Change the Authentication to Individual User Accounts. Notice and memorize that the API option is in the same view as the Web MVC menu. It's easy to forget.
Click OK and it will scaffold out the Web API project.
Important before running:
Copy the connection string from the ElevenNote.WebMvc Web.config
file. This is the same we've been using throughout the app.
Paste it into the ElevenNote.WebAPI Web.config
file in place of what is there
If you haven't already, you can remove the .mdf file from the connection string.
Set ElevenNote.WebAPI as the startup project
Right click on ElevenNote.WebAPI and choose Add > Reference
Select the Data, Models, and Services projects.
Open ElevenNote.Data -> IdentityModels.cs.
Paste this code inside the ApplicationUser
class:
Here is a screenshot for context:
Open ElevenNote.WebAPI -> App_Start -> WebApiConfig.cs
This handles the routing of the API routes
Run the app
Click on API
This is an automatically generated document listing your API endpoints
Close the app
Go to ElevenNote.WebAPI -> Models and delete the IdentityModels.cs
file, this is already in the database
CTRL SHIFT B
to build the API. You should have a load of errors. Fix all of the errors with CTRL .
to bring in the using statements. This is very similar to what you had to do when you created the MVC project. Notice in the screenshot that IdentityModels.cs has been deleted in the WebApi project.
Next, we'll test the API with Postman.