Introduction to Web Services in .NET with Web API Example

Introduction

A Web Service is an interface which is accessible on the network to application programs. Web Services are built using standard Internet technologies. Clients of a web service don’t need to know the implementation of the web service. The only thing the clients of the web service needs to know is the interface of the web service through which they can communicate with the web service. Now Web API is used to create web services in .NET.

Definitions

Web service is a software resource that is URL-addressable which can perform some function/s.

Web Services are a new breed of Web Applications which are self-contained and self-describing modular applications. They can be published, located and invoked across the web to perform some task that can be anything from simple requests to complicated business processes. Once the web service is deployed, other applications (can be web services) can discover and invoke the deployed service

Web API in .NET

Web API is used to implement RESTful web services in .NET. It is based on modular, pluggable pipeline model. When the server with Web API receives a request, it is first passed through .net request pipeline which provides the ability to implement your modules if the default functionality is not enough. You can also host your Web API outside the windows server.

Web API uses MVC Controller and Action concepts. The resources can be directly mapped to the controller. It uses .NET routing engine to map URLs to the controllers. APIs are places within ‘/api/’ route which help to separate API and non-API.

Web API Architecture
Web API Architecture

Web API Example

Download Web API 2 Demo Project

Create a new empty web application. Add required folders i.e. Model and Controllers.

Model

The model of your application will represent the data. Wen API in .net can automatically serialize Model to JSON or XML and writes the data to HTTP response message. The client can indicate the desired format by setting the Accept header in HTTP request message.

Create the Model of the application.

Add Model class
Add Model class

Add properties to the class.

EmployeeModel.cs

 

Controller

The Controller handles HTTP requests.

Create a new Web API 2 Controller – Empty.

Add a new Web API 2 Empty Controller
Add a new Web API 2 Empty Controller

Rename the controller as required. Your controller will extend ApiController. 

Now add a static list of employee data. (you can also load data from the database in the dataset)

Implement methods to get all the records in the form of IEnumerable<EmployeeModel> and another method that returns single user against some Id.

Return Complete List

Async Secure Response for complete records

If you want to read Async Response. i.e. all the employees

Get Single Record Against Id

Search the list and return the record against a specific record Id. i.e. employee Id

EmployeeController.cs

 

Until now, you have successfully created a Web Service using Web API now you can access your Web API from the browser through localhost.

Web API return XML
Web API return XML

As well as, you can also get a single employee record.

Web API return single record in XML
Web API return single record in XML

Web API Return JSON Only

Your Web API can return JSON only if you want. To return only JSON from the web service open App_Start > WebApiConfig.cs and add JSONFormatter.

Web API Return JSON and XML Both

Web API can also return both XML and JSON depending upon the request. If you want to return both XML and JSON open Global.asax file. Add XMLFormatter and JSONFormatter.

Now your Web API can respond to both XML and JSON requests.

  • If you want JSON in response, append ?json=true at the end of the URL i.e. http://localhost:25600/api/employee?json=true
  • If you want XML in response, append ?xml=true at the end of the URL i.e. http://localhost:25600/api/employee?xml=true

Call Web API with AJAX and jQuery

Add a new web page and create an ordered list to display the list of employees and input field to filter employees with the help of Id.

Populate the list from the response of AJAX request, also get a single record when the user clicks on Search button based on Id.

index.html

Download Web API 2 Demo Project

Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!