Get Data From Table Using ASP.Net WEB API

ASP.Net WEB API

The ASP.Net Web API is a framework that makes it easy to build REST Services that reach a broad range of clients, including browsers and mobile devices. It's an ideal platform for building Restful Services on the .NET Framework.

The following are the features of the ASP.Net WEB API:

  • Strong support for URL Routing to produce clean URLs using familiar MVC style routing semantics
  • Content Negotiation based on Accept headers for request and response serialization
  • Support for a host of supported output formats including JSON, XML and ATOM
  • ODATA Support
  • Strong default support for REST semantics
  • Very extensible, based on a MVC-like extensibility model of Formatters and Filters
  • Self-hostable in non-web applications
  • Testable using testing concepts similar to MVC

Demo

Let's create a database table using the following fields and insert some dummy data. 

 

Now create project of the WEB API. I am using Visual Studio 2013 but you can also create it in Visual Studio 2012.

 
 
 

Once you create it look into the Controllers folder where you see ValuesController class that is the default. This class has all the default methods of the WEB API.

 
Now your project is ready so it's time to write the code. The following is the sequence of components that we will write.
 
 

Create Entity Framework Class

 
 
 
 
 

Now write the repository class that will call the Entity Framework class and used for database interactions.

 
 
Then write the code on the Valuescontroller class that is actually used for communication for the client and consumed.
 
 
 
You can run the preceding code and see the data in the format of XML.
 
 
Now it's time to write the frontend code using JavaScript that will call that WEB API.
 
 
 
 

Call the Demo View from the Home Controller.

 

Now run the application.

 
 
 
 
Please find the attached source code for the preceding demo.The source code contains all the CRUD operations.
 
Thanks for reading. 


Similar Articles