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

Classes in C# Explained

C# Class Explained A class is nothing but an encapsulation of properties and methods that are used to represent a real-time entity, as explained by Guru99 . For instance, if you want to work with Guest’s data as in our previous DataDriven Web application . The properties of the Guest would be the Id, GuestName, Address, Phone number etc of the Guest. The methods would include the entry and modification of Guest data. All of these operations can be represented as a class in C# as shown below. using System; namespace CsharpnaijaClassTutorial {     public class Guest     {         public int Id { get ; set ; }         public string GuestName { get ; set ; }         public string Address { get ; set ; }         public string WhomToSee { get ; set ; }     ...

ASP.NET MVC Routing

ASP.NET MVC Routing ASP.NET MVC routing is a pattern matching system that is responsible for mapping incoming browser requests to specified MVC controller actions. When the ASP.NET MVC application launches then the application registers one or more patterns with the framework's route table to tell the routing engine what to do with any requests that matches those patterns. When the routing engine receives a request at runtime, it matches that request's URL against the URL patterns registered with it and gives the response according to a pattern match. Routing pattern is as follows A URL is requested from a browser, the URL is parsed (that is, break into controller and action), the parsed URL is compared to registered route pattern in the framework’s route table, if a route is found, its process and send response to the browser with the required response, otherwise, the HTTP 404 error is send to the browser. Route Properties ASP.NET MVC routes are res...

ASP.NET MVC Views

Views in ASP.NET MVC Application explained Find a related article By  Steve Smith  and  Luke Latham from Microsoft Corporation here In the Model-View-Controller (MVC) pattern, the  view  handles the application's data presentation and user interaction. A view is an HTML template with embedded  Razor markup . Razor markup is code that interacts with HTML markup to produce a webpage that's sent to the client. In ASP.NET MVC, views are  .cshtml  files that use the  C# programming language  in Razor markup. Usually, view files are grouped into folders named for each of the application's  controllers . The folders are stored in a  Views  folder at the root of the application as shown: The  Home  controller is represented by a  Home  folder inside the  Views  folder.  The  Home  folder contains the views for the  About ,  Contact , and  Index...