Build a RESTful Web API with ASP
Build a RESTful Web API with ASP
ASP.NET Core 6
Sena Kılıçarslan
·
Follow
Published in
.NET Core
·
12 min read
·
Jun 29, 2022
711
10
What is REST?
Creating a Web API Project
Adding a Model
What is REST?
RESTful APIs conform to the REST architectural style.
REST has had such a large impact on the Web that it has
mostly displaced SOAP-based interface design because it’s a
considerably simpler style to use.
Also, you can use the curl URL shown in the Swagger UI for
this method and see the result of the URL in the browser:
When we run the application, the default URL comes from
the launchSettings.json:
launchSettings.json
Adding a Model
Now, we will implement our data model class.
In Solution Explorer, right-click the project. Select Add -
> New Folder and name the folder Models.
Movie.cs
MovieContext.cs
appsettings.json
Dependency Injection
Program.cs
Creating Database with Migrations
Now, we will create the database using the EF
Core Migrations feature.
Add-Migration Initial
Update-Database
MoviesController.cs
As you see, the class is decorated with
the [ApiController] attribute. This attribute indicates that the
controller responds to web API requests.
MoviesController.cs
GET Method
GET /api/Movies
GET /api/Movies/{id}
https://localhost:{port}/api/movies
https://localhost:{port}/api/movies/{id}
Select the first GET method and click Try it out -> Execute:
This shows all of the movies in the application.
Next, click the second GET method and click Try it out and
enter one of the Ids above in the id field and click Execute:
Click Try it out and enter the movie information that you
want to add in the request body:
and click Execute.
We can paste this location URL in the browser and see the
response there too:
Also, we can check this record from the Movies table in our
local database:
PUT Method
Click Try it out and enter the movie information that you
want to update in the request body and the Id of the movie in
the id field:
DELETE Method
Click Try it out and enter the Id of the movie you want to
delete in the id field:
This is where this post ends. You can find the full project in
this GitHub repository.
I hope you found this post helpful and easy to follow. Please
let me know if you have any corrections and/or questions in
the comments below.
https://medium.com/net-core/build-a-restful-web-api-with-asp-net-core-6-30747197e229
https://medium.com/net-core/build-a-restful-web-api-with-asp-net-core-6-30747197e229