ASP.NET Web API Explained
What is Web API?
The
ASP.NET Web API is a framework that makes it easy to build HTTP services that
reach a broad range of clients, including browsers and mobile devices.
The ASP.NET Web API is
an ideal platform for building Restful applications on the .NET Framework.
Why do we advocate for the use of Web API?
Nowadays, most mobile
devices, browsers and tablets are the medium for accessing most of the
internet and also, people are using mobile apps the most and to provide data to
apps.
When are we to use Web API?
Whenever we want to
expose the data/information of our application to our clients and other people,
then those people can use our data and interact with the data/information we
expose to them.
For instance, a mobile
application requires a service, HTML 5 also requires a service as well as
desktop and tablets require services. Most current device apps require Web API
services.
The ASP.Net Framework
leverages both web standards such as HTTP, JSON and XML and it provides a
simple way to build and expose REST based data services.
Some core concepts of
ASP.Net MVC are similar to the ASP.Net Web API such as routing and controllers.
Building a Web API
Let us develop an ASP.NET
Web API using Visual Studio 2019.
Create a new Project
and select ASP.NET Web Application (.Net Framework) and click no Next as
shown below
Provide the name of
the project as in CsharpNaijaWebAPI and click on Create Button as shown below
Click on Change link
button from the top right corner and on Individual User Accounts and then click
on as shown below
Select MVC and click
on Create button as shown below
A new ASP.NET MVC
application we be created as shown below
New let us create a
Web API in the solution that we created as in the above.
Right click on the
solution name select Add then New Project as shown below
Select ASP.NET Web Application (,NET Framework)
as we did in the above.
Change the
authentication type to Individual User Accounts for User authentication and select
the Web API and click on create button as shown below
Now we have created
two projects in our solution namely; CshapnaijaWebAPI and WebAPI
Expand the Controllers
folder and double click on ValuesController to see the default controller
action methods generated when creating the Web Api.
We have successfully
added a Web API controller to your application.
Now you can build and
run the application, the output is as shown below
Click on the API menu
to see the default Api controller generated for us as shown below
It's easy to configure it as a Web API.
The ASP.Net MVC
and ASP.Net Web API makes heavy use of convention for configuration to lighten
the work load for creating the services.
From the code snippet
below we can see the http verbs for CRUD operation on our data
The HTTP actions and
their corresponding CRUD operations are:
- GET (Read)
Retrieves the representation of the resource.
- PUT(Update)
Update an existing resource.
- POST (Create)
Create new resource.
- DELETE (Delete)
Delete an existing resource.
We will continue with
ASP.NET Web API in the next article
Hope this article
really throw some light on the concept of Web API
Comments
Post a Comment