Creating An API
Creating The Service
There are numerous
methods to develop a service, including REST or possibly gRPC. REST, which
stands for REpresentational State Transfer, is essentially a method for
computers to communicate with one another using HTTP. In REST, various HTTP
verbs are utilized for distinct operations. It could look something like this:
|
URI |
Verb |
Action |
|
/BlogPosts |
Get |
Gets
a list of blog posts |
|
/BlogPosts |
Post |
Creates
a new blog post |
|
/BlogPosts/{id} |
Get |
Gets
a blogpost with a specific ID |
|
/BlogPosts/{id} |
Put |
Replaces
a blogpost |
|
/BlogPosts/{id} |
Patch |
Updates
a blogpost |
|
/BlogPosts/{id} |
Delete |
Deletes
a blogpost |
This is what we are going
to implement for tags, categories, and blog posts. Since the API determines if
the post should be created, we will only implement the Put (replace)
since we cannot ascertain whether we are creating new data or updating existing
data. The API will exclusively be utilized by Blazor WebAssembly, thus we will
develop the API within the MyBlogWebAssembly.Server project.
Adding Database Access
Execute the following
steps to provide database access:
- In the MyBlogWebAssembly.Server
project, open Startup.cs.
- Add a reference to the MyBlog.Data project by right-clicking Dependencies beneath the MyBlogWebAssembly.Server project and selecting Add project reference.
- Check MyBlog.Data and click Ok.
- Add namespaces.
We have now incorporated
access to the classes from the MyBlog.Data project.
We have set it up so that
when we request an instance of IMyBlogApi, we receive an instance of the
MyBlogApiServerSide class. This is due to our server-side context,
allowing the API to connect directly to the database.
Conclusion
Starting process of
creating an API is discussed.
Comments
Post a Comment