Web API is built to work with HTTP. It is an ideal platform for building RESTful services. It provides actions that support the HTTP verbs, such as GET, POST, PUT, and DELETE. It supports a broad range of clients, including desktops, tablets, and pones. It provides built-in features for routing, action results, and more, and it’s clean patterns and samples make ASP. NET Web API relatively easy to learn. If you already use ASP. NET Core MVC, ASP. NET Core Web API is similar, and you can apply much of what you already know to Web API.
REST is used to build Web services that are lightweight, maintainable, and scalable in nature. A service which is built on the REST architecture is called a RESTful service. The underlying protocol for REST is HTTP, which is the basic web protocol. REST stands for REpresentational State Transfer.
REST is a way to access resources. The key elements of a RESTful implementation are as follows:
Let’s assume that we have a RESTful web service is defined at the location. http://dotnetacademy.be/employee . When the client makes any request to this web service, it can specify any of the normal HTTP verbs of GET, POST, DELETE and PUT. Below is what would happen If the respective verbs were sent by the client.
ASP.NET Core 2.1 introduced support called IActionResultExecutor<T>. It allows us to use some of the action results -those that we are used to from MVC controllers – outside of the controller context, so for example from a middleware component.
The most important of the ActionResult family is the ObjectResult – which internally handles content negotiation – so determines what media type is suitable for the response, and serializes the response accordingly using the selected formatter (JSON, XML, Protobuf or whatever you support). The typical controller, using an ObjectResult might look like this:
Extra reading material:
[HttpGet]
public IActionResult Get()
{
var projects= new[]
{
new Project { Id = 1, Name = "a" },
new Project { Id = 2, Name ="b"}
};
return new ObjectResult(projects);
}
or,
[HttpGet]
public IEnumerable<Projects> Get()
{
var projects= new[]
{
new Project { Id = 1, Name = "a" },
new Project { Id = 2, Name ="b"}
};
// will do content negotiation
return projects;
}
Finally, you could replace our usage of ObjectResult, or the POCO, with a call to Ok() on the base controller:
[HttpGet]
public IActionResult Get()
{
var projects= new[]
{
new Project { Id = 1, Name = "a" },
new Project { Id = 2, Name ="b"}
};
return Ok(projects);
}
Our temporary controller (just to test):
[Route("api/[controller]")]
[ApiController]
public class ProjectsController : ControllerBase
{
// IProjectQuery query;
public ProjectsController()
{
}
/* [HttpGet]
public IQueryable<Project> Get()
{
var result = query.Get();
return Ok(result);
}*/
[HttpGet]
public IActionResult Get()
{
var projects = new[]
{
new Project { Id = 1, Name = "a" },
new Project { Id = 2, Name ="b"}
};
return Ok(projects);
}
}
If you run the project, you also can start yarc (it’s a chrome extension to test your Rest API calls: