In this asp.net c# rest web service tutorial you will learn how to develop restful service for their application.
Restful service has been so popular and widely accepted by all developers’ community for following reasons.
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 } ); } }
An architectural style called REST (Representational State Transfer)
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