What is WebAPI
WebAPI in .NET Core is used for creating RESTful services.WebAPI in .NET Core is a class which derives from the ControllerBase class.This is unlike MVC controller which derives from Controller class.Controller class adds support for working with views.
Different return types
we can return the following from the controller action method:
- Specific type primitive or complex data type
- IActionResult interface
- ActionResult<T> for returning a type deriving from ActionResult or specific type
We can return either IActionResult or ActionResult<T>,either synchronously or asynchronously.To call these methods asynchronously follow the below steps:
- Add the async modifier in the method definition
- Return Task<IActionResult> or Task<ActionResult<T>>
- Call the asynchronous method using the await keyword
If the requests are processed asynchronously then the server resources are not blocked and can be used to serve other request.Threads can be used serve other requests.
To create an async action method:
public async Task<IActionResult> SimeMethodAsync() { await asyncOperation.method1(); return OK(); }
Controller class provides additional functionality for working with MVC applicaitons.Some of the properties and methods provided by ControllerBase are:
- BadRequest Returns 400 status code.
- NotFound Returns 404 status code.
- PhysicalFile for returning a file.
- TryUpdateModelAsync for invoking model binding.
For example to return a response when a value is not found:
[HttpGet] public IActionResult FindValue(int id) { ... return NotFound(); }
How to create WebAPI in ASP.NET Core
- To create a WebAPI controller class create a normal class and add the [ApiController] attribute to the class.This attributes enables API specific behaviours.
- Add a method to the Contoller class deriving from ControllerBase
- Add the HttpPost or HttpGet attribute to the method
- Call the CreatedAtAction prebuilt method The CreatedAtAction method created ActionResult object that returns 201 response.The 201 response type denotes that the resource is created
[HttpPost] public IActionResult Create(Employee employee) { ... return Ok(); }
or if you want to create a resource and provide access to it you use CreatedAtAction method.This method adds location header in the response which can be used to access the newly created resource:
[HttpPost] public IActionResult ActionResult<Employee> Create(Employee employee) { return CreatedAtAction("createEmployee", new { id = 1 }); }
Some of the useful attributes while developing APIs are:
- [Produces] specifies the expected return Types from action method
- [Consumes] specifies the expected input Types by action method
- [Route] for defining routes.
- [HttpGet] for defining get verb routes
- [HttpPost] for defining post verb routes
To return an employee using Get:
[HttpGet("{id}")] public async Task<ActionResult<Employee>> GetEmployee(int id) { //fetch employee return employee; }
We are calling this method asynchronously and returning task.
Leave a Reply