Asp.net c# restful service example

In this asp.net c# rest web service tutorial you will learn how to develop restful service for their application.

Why Restful so popular

Restful service has been so popular and widely accepted by all developers’ community for following reasons.

  • It easily allow to communicate different type of web applications that are built on various programming languages
  • There is no environment dependency; all application can reside in different type of system and still can exchange data
  • Without revealing internal mechanism security can be implemented easily

We can create rest api in .net framework either by inheriting from ApiController or from ControllerBase class.

In .net core framework 3.1, we can define API class in following way

[ApiController]
[Route("[controller]")]
public class StudentController : ControllerBase
{   
    [HttpGet("{id}")]
	public async Task<Student> Get(int id)
	{
		Student student = new Student();		
		student.StuId = id;    
		return await Task.FromResult(student);
	}
}

In .net framework 4.5, API controller defined is differently

public class StudentController : ApiController
 {
        
	[HttpGet]
	public async Task<Student> Get(int id)
	{
		Student student = new Student();       
		student.StuId = id;
		student.City = "Kolkata";         
		return await Task.FromResult(student);
	}
}

Also, you need to add routes.MapHttpRoute method in RouteConfig file.

public class RouteConfig
{
	public static void RegisterRoutes(RouteCollection routes)
	{
		routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

		routes.MapHttpRoute(
		  name: "DefaultApi",
		  routeTemplate: "api/{controller}/{id}",
		  defaults: new { id = RouteParameter.Optional }
	  );
	}
}

RESTful Key Characteristic

An architectural style called REST (Representational State Transfer)

  • Client-Server
  • Cacheable
  • Stateless
  • Layered
  • Uniform Interface
  • HTTP protocol is used
RESTful Service Methods

RESTful service is type of Web services that are lightweight, maintainable, and scalable in nature. Any service which is built on the REST architecture is called a RESTful service. REST service also works on HTTP protocol.

We learn how to create a REST Service with Asp.Net

  1. Get Method Example - This method is used to get a list of all objects (data)
  2. Post Method Example - This method is used for posting a new element to be added in database
  3. Put Method Example - This method is used for posting a element to be updated in database
  4. Delete Method Example - This is used to delete an element from database
  5. Asp.net rest service security example - Learn, how to implement authentication in resetful web api
  6. Testing Web API- Learn how to test Asp.net Web API using Fiddler
  7. Asp.net consume rest web service - Call web api from asp.net client
  8. Calling rest service from c# console application

 
Asp.Net Web API Rest Service
Learn Web API: creating API using C# .Net, post, update, delete, read using .net API, set security, check authentication, testing.

C# web service interview questions

Learn API development
Asp.Net C# Web API Examples | Join Asp.Net MVC Course