In this article, I will teach how to create API using Visual Studio Code. For this, you need to install VSCode at this link. In this case, I'm using de NET version 5.0 but you can use the version .NET 3.0.
First step: First of all, before installing the VSCode open the Windows Power Shell and select your directory, the directory where you will create this project API.
Second step: Write in the Power Shell the command dotnet new webapi -o CourseApi. Where the syntax -o is used for creating the directory for your project.
Third step: Open the Visual Studio Code and select your directory, the directory where we created the project Web API. Delete the files WeatherForecast.cs and WeatherForecastController.cs.
Now we let´s create some folders and classes. Lets to create the folder: Model and Data.
We will create the classes Category and Course. How you can see in the image below, we will use the validation in properties using DataAnnotations in fields.
- Category { Id, Title and CategoryName }
- Courses {Id, Name, Description, Price, CategoryId and Category }
- DataContext - for to work EntityFrameworkCore and We will using database in memory.
Category
Course
Now let´s create the class DataContext for we work with Entity Framework Core, so write the terminal console VSCode this command for add package EF in memory run dotnet add package Microsoft.EntityFrameworkCore.InMemory, as in the image below.
Fourth Step: Let´s configure the Startup class for we need to say for our application, that we are work with Entity Framework and configure. The services are registered ConfigureServices and consumed throughout the application through dependency injection (di) or ApplicationServices.
Fifth Step Let´s create the controllers, CartegoryController and CourseController.
CategoryController
Sixth Step Now we need to test our application.
You can run your API with Ctrl+F5 and open the Postman and write the address https://localhost:5001 (HTTPS) or https://localhost:5000. This test we will testing the verbs POST and GET, but you can create other verbs for this application too.
Test
CoursesController
Test
References
DataContext in portuguese
Startup Class in portuguese
Startup Class in english
DataContext in english
Entity Framework tutorial
Top comments (5)
the third step could be the commands "cd CourseApi" and "code ."
This was great, Thank you!
Great thanks you
your wellcome :)
Congrats Jéssica!