Skip to main content

ASP.NET Web API Explained


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

Popular posts from this blog

Collections in C#

Collections in C# In our previous article , we have learned about how we can use arrays in C#. Arrays in programming are used to group a set of related objects. So one could create an array or a set of Integers, which could be accessed via one variable name. What is Collections in C#? Collections are similar to Arrays, it provides a more flexible way of working with a group of objects. In arrays, you would have noticed that you need to define the number of elements in an array beforehand. This had to be done when the array was declared. But in a collection, you don't need to define the size of the collection beforehand. You can add elements or even remove elements from the collection at any point of time. This article will focus on how we can work with the different collections available in C#. There are three distinct collection types in C#: standard generic concurrent The standard collections are found under the System.Collections. They do not store elemen...

The String.Join Method in C# Explained

The String.Join Method in C#   The string.Join concatenates the elements of a specified array or the members of a collection, using the specified separator between each element or member. Overloads of string.Join Method Description Join(Char, Object[]) Concatenates the string representations of an array of objects, using the specified separator between each member. Join(Char, String[]) Concatenates an array of strings, using the specified separator between each member. Join(String, IEnumerable<String>) Concatenates the members of a constructed IEnumerable<T> collection of type String, using the specified separator between each member. Join(String, Object[]) Concatenates the elements of an object array, using the specified separator between each element. Join(String, String[]) Concatenates all the elements of a string array, usi...

System.IO Namesapce in C#

  System.IO Namesapce in C# A  file  is a collection of data stored in a disk with a specific name and a directory path. When a file is opened for reading or writing, it becomes a  stream . The stream is basically the sequence of bytes passing through the communication path. There are two main streams: the  input stream  and the  output stream . The  input stream  is used for reading data from file (read operation) and the  output stream  is used for writing into the file (write operation). From the above definition of file, the C# provides a namespace that enable us to manipulate file in C# called System.IO.   System.IO  is a  namespace  and it contains a standard IO (input/output) types such as classes , structures , enumerations , and  delegates  to perform a read/write operations on different sources like file, memory, network, etc.   System.IO Classes The table below shows differen...