1.
Create an employee
[HttpPost]
public async Task<IActionResult> CreateEmployee(Employee employee)
{
if (employee == null)
return BadRequest("Employee data missing");
_context.[Link](employee);
await _context.SaveChangesAsync();
return Ok();
return Ok(employee);
return Ok("Success");
return Ok(new
{
Message = "Employee created successfully",
Data = employee
});
Why?
Ok() is a method defined in ControllerBase class.
Ok() → returns HTTP 200 OK
Ok(object) → returns HTTP 200 + JSON body
What is IActionResult? IActionResult is an interface in [Link] Core that
represents different types of HTTP responses.
like Ok() BadRequest() NotFound() Created() Unauthorized()
If return type is IActionResult, you can return any valid HTTP response.
If your method returns only one type (like Employee), then use:
Task<Employee>
But if you want flexibility (Ok, NotFound, BadRequest…), use:
Task<IActionResult>
1. async keyword : indicates methods contains asynchronous operations and run
without blocking the main thread.
So Task<IActionResult> means:
“An asynchronous method that returns an HTTP response.”