Part - 8 Controllers in ASP.NET Core Web API
Part - 8 Controllers in ASP.NET Core Web API
Part - 8 Controllers in ASP.NET Core Web API
The Controller class in ASP.NET Core Web API must have a “Controller” suffix.
In ASP.NET Core Web API, the Controller class must be inherited from the ControllerBase class. If
you are coming from an ASP.NET Core MVC background, then, in that case, the Controller class is
inherited from the Controller class.
API Controller with Read/Write Actions: This type of controller includes basic CRUD
(Create, Read, Update, Delete) operations. It's suitable for applications where you need to
manage data records, providing methods for listing, fetching, creating, updating, and deleting
data.
API Controller with Actions using Entity Framework: This template extends the CRUD
functionality by integrating Entity Framework, which is an Object-Relational Mapping (ORM)
framework. It allows you to interact with your database using .NET objects, making it easier to
perform data operations without writing a lot of SQL code.
API with Read/Write Endpoints: Similar to the API Controller with Read/Write Actions, this
controller type focuses on providing endpoints for reading and writing data. It's a more
streamlined version focused on these two operations, ideal for simpler APIs where complex
operations like updating or deleting are not required.
API Controller with Read/Write Endpoints using Entity Framework: This is a more
advanced version that combines the simplicity of read/write operations with the power of
Entity Framework. It's well-suited for applications that require straightforward data access but
also benefit from the efficiencies of an ORM.
namespace MyFirstWebAPIProject.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class EmployeeController : ControllerBase
{
[HttpGet]
public string Get()
{
return "Returning from EmployeeController Get Method";
}
}
}
As per Web API standard, each resource should have a Unique Identifier. Let us do some changes in
our Route Attribute so that each request will have a unique URI. In the Route attribute, we specified
the action as part of the URI. In our upcoming session, we will discuss Routing in detail. As of now,
just modify the EmployeeController class as shown below.
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace MyFirstWebAPIProject.Controllers
{
[Route("api/[controller]/[action]")]
[ApiController]
public class EmployeeController : ControllerBase
{
[HttpGet]
public string Get()
{
return "Returning from EmployeeController Get Method";
}
[HttpGet]
public string GetEmployee()
{
return "Returning from EmployeeController GetEmployee Method";
}
}
}
With the above changes in place, now run the application. Now we have to pass the method name as
part of the URL as shown in the below image.
api/employee/get
api/employee/getemployee
Note: In this session, I have just given an introduction to Controllers and in our upcoming sessions,
we will discuss how to implement GET, POST, PUT, PATCH and DELETE Methods in a proper
manner.
ApiController Attribute in ASP.NET Core Web API
The ApiController attribute in ASP.NET Core Web API is a feature introduced in ASP.NET Core 2.1 to
enhance the development of HTTP Based APIs. It is applied at the class level and offers several
conveniences and conventions that make it easier to build clean and well-documented Web
APIs. Here are some key aspects of the ApiController attribute:
Attribute Routing Requirement: With ApiController, attribute routing becomes a
requirement. This means you must use [Route], [HttpGet], [HttpPost], etc., to define the
routes for your actions.
Automatic HTTP 400 Responses: It automatically handles model validation errors by
providing a standardized response. If the request model does not satisfy the model
validation rules, the API responds with a 400 Bad Request, without writing additional
code.
Binding Source Parameter: The attribute infers the source of parameters for actions. For
instance, it assumes [FromBody] for complex type parameters and [FromQuery] for simple
types in GET requests. This reduces the need to explicitly specify the source of the
parameter.
Enhanced Swagger Support: When used in conjunction with tools like Swashbuckle, it
provides better metadata for API documentation. This results in more descriptive Swagger
UIs.