0% found this document useful (0 votes)
12 views1 page

Create Employee in ASP.NET Core

The document outlines an ASP.NET Core method for creating an employee, detailing the use of IActionResult to return various HTTP responses. It explains the significance of the Ok() method for returning HTTP 200 responses and the async keyword for asynchronous operations. The method can return different types of responses, providing flexibility in handling various outcomes of the employee creation process.

Uploaded by

Soya Bean
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views1 page

Create Employee in ASP.NET Core

The document outlines an ASP.NET Core method for creating an employee, detailing the use of IActionResult to return various HTTP responses. It explains the significance of the Ok() method for returning HTTP 200 responses and the async keyword for asynchronous operations. The method can return different types of responses, providing flexibility in handling various outcomes of the employee creation process.

Uploaded by

Soya Bean
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

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.”

You might also like