1. Back To Blog

ASP.NET MVC Interview questions: - What is WebAPI in MVC ?

What is "WebAPI"?

 

HTTP is the most used protocol. For past many years browser was the most preferred client by which we can consume data exposed over HTTP. But as years passed by client variety started spreading out. We had demand to consume data on HTTP from clients like mobile,javascripts,windows application etc.

 

For satisfying the broad range of client "REST" was the proposed approach. You can read more about "REST" from WCF chapter.

 

"WebAPI" is the technology by which you can expose data over HTTP following REST principles.

 

But WCF SOAP also does the same thing, so how does "WebAPI" differ?


 

With WCF also you can implement REST,So why "WebAPI"?


 

WCF was brought in to implement SOA, never the intention was to implement REST."WebAPI'" is built from scratch and the only goal is to create HTTP services using REST.


 How to implement "WebAPI" in MVC?

 

Below are the steps to implement "webAPI" :-

 

Step1:-Create the project using the "WebAPI" template.




Step 2:- Once you have created the project you will notice that the controller now inherits from "ApiController" and you can now implement "post","get","put" and "delete" methods of HTTP protocol.

public class ValuesController : ApiController

   

        // GET api/values

        public IEnumerable Get()

       

            return new string[] "value1", "value2" };

        }

 

        // GET api/values/5

        public string Get(int id)

       

            return "value";

        }

 

        // POST api/values

        public void Post([FromBody]string value)

       

        }

 

        // PUT api/values/5

        public void Put(int id, [FromBody]string value)

       

        }

 

        // DELETE api/values/5

        public void Delete(int id)

       

        }

    }

 

Step 3:-If you make a HTTP GET call you should get the below results.


MVC (Model view controller) interview question References for further reading:-

 

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social