1. What is ASP. NET Core Web API

What is the need for a web api?

What is a web api?

Why using ASP. NET Core Web API as the backend web service? 

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. 

What is HTTP

What is a http request

What is a Restful Web Service?

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:

  • Resources: The first key element is the resource itself. Let assume that a web application on a server has records of several employees. Let’s assume the URL of the web application is http://dotnetacademy.be . Now in order to access an employee record with id 1 resource via REST, we go: http://dotnetacademy.be/employee/1 : This command tells the web server to please provide the details of the employee whose employee number is 1.
  • Request Verbs : These describe what you want to do with the resource. A browser issues a GET verb to instruct the endpoint it wants to get data. However, there are many other verbs available including things like POST, PUT, and DELETE. So in the case of the example http://dotnetacademy.be/employee/1, the web browser is actually issuing a GET Verb because it wants to get the details of the employee record.

Restful Methods

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.

  • POST – This would be used to create a new employee using the RESTful web service
  • GET – This would be used to get a list of all employee using the RESTful web service
  • PUT – This would be used to update all employee using the RESTful web service
  • DELETE – This would be used to delete all employee using the RESTful web service

What are HTTP verbs?

What are status codes?

Frequently used status codes

An elegant way of producing HTTP responses in ASP.NET Core outside of MVC controllers

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.

Controller helper methods

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: