Posted on 4/8/2020 10:32:08 PM by Admin

Asp.Net Web API

Before learning Asp.Net Web Api ,we will have to understand the term API.API stands for application programming interface. You must have heard about Google Map API or YouTube API. Have you ever thought why these well known companies makes APIs, when they already have damn good Applications with rich user interfaces.

The name API itself says a lot, it is for us meaning for developers who create applications and Google and Facebook are giving us the opportunity to use their readymade services in our application. See, I said Applications ,I did not specify which type of application, a mobile App, a desktop App , a web App or IOTs, IOTs are those devices which can communicate over internet ,many modern cars and electronic gadgets use this technology.

This is the reason we see Google map services available in android application, IOS application or any web applications.

In short APIs are platform independent and language independent that is why can be consumed by a browser, a JavaScript application or any application developed in any language.

What is Web API

Web Api is a Asp.Net technology used to create REST services with any .Net Language. As it is an Asp.Net technology ,it inherits many of its abilities automatically. Now, for a minute exclude the word REST from your mind and just think about service. A postal service, a pizza delivery service or any service ,why are these called service ,what is common among them ,the thing in common is they all are fulfilling some requirement completely besides they are completely independent meaning to order a pizza you just have to tell which type of pizza and that is it, you will not worry about who is the cook ,and how is their kitchen and who is doing the packaging right? So a service means a self contained independent functionality. In the world of web , if you are able to consume any such service (method) from any device then it means you have developed an API.

Now if you have developed any service following all the REST principles then you have developed a rest service. Asp.Net web API helps you to create services which follows all the rest principles obediently. I think , it is not important to tell you that any service just exposes data to the outside world and not the UI ,and how the consumer presents those data to their user, what are they going to do with those data is completely up to the consumer of your API.

Rest and Rest services

Rest stands for representational state transfer. It is actually an architectural style built on certain principles. Remember, if anyone says architectural style it means it is a theory or concept and how will you use that theory is up to you. For example Layered architecture is a theory ,it never says how many layer ,and how one layer should communicate with other and it completely depends on the developer how he take this theory to implementation. On the other hand MVC is an architectural pattern meaning everything is pre decided that it will have three layers and how the layer will communicate among themselves.

The REST principles are:-
  1. Everything is a resource.
Meaning, whatever is available on web is a resource. If you are trying to get a Video then video is a resource ,if you are trying to get list of employees then List of Employee is a resource.
URL- http://mycom.com/video/1 is indicating a video resource.Similarly
URL- http://mycom.com/images/flower.png is an image resource.
  2. There must be a URI for each resource.
Meaning, each resource must be uniquely identifiable with a unique address.
  3. Simple and unique interfaces.
Meaning each URL must be simple and meaningful, which indirectly indicating method names should be uniform . You can achieve it with Http verbs.
URL- http://mycom.com/GetEmployee and http://mycom.com/ShowEmployee ,both are serving same purpose ,so it is better to make them more simple like http://mycom.com/employee (http get)
  4. Request and Response must be done with representation.
As ,services can be consumed by any technology , it is better to follow some standard that is actually a representation .For example an Employee detail like below


<employee>
    <name>Sachin</name>
    <age> 24</age>
</employee>

is an XML representation. similarly

  {
  name:"sachin",
  age:"24"
   }

is a JSON representation.
  5. Stateless.
Meaning ,each request will be a fresh request following the http principle.

If you have created the service following each of the above points then you have created a RESTful service.