MVC (Model-View-Controller) REST API
MVC is a classical software design pattern to structure the creation of an user interface providing access to a large and convoluted data set. With the context of web APIs specific specializations apply:
Controller
have the purpose to translate the data carried by a HTTP request into
the representation used with the application and to control or invoke
functional application services that actually carry out the domain
specific operations needed to satisfy the API request.
Controllers are implemented by classes derived from
Tlabs.Server.Controller.ApiCtrl that contain public (action) methods
which are bound to handle specific HTTP requests. The action method’s
parameters are bound to data contained in the request.
View
The View part of an API is represented with the JSON representation of
the data returned from an API. This is supported by simply representing
any data returned from a Controller-Action that is NOT a
ActionResult
as JSON encoded data.
In order to unify the the structure of the data returned by an API, the
JSON data is typically wrapped into a so called Cover (see below).
Model
In contrast to the original definition of the MVP pattern, with Model we
simply denote the data types or (data transfer objects, DTO) that are
defined with a particular API to describe the data being passed or
returned. Thus the Model typically consists of the more complex data
types being passed to or being returned by application services called
from the controller action method.
A very basic implementation of a web API from HTTP GET api/samples
would look like:
[Route("api/[controller]")]
public class SamplesController : Microsoft.AspNetCore.Mvc.Controller {
[HttpGet]
public SampleModel[] Get() {
return new SampleModel[] {
new SampleModel() {
Prop1= "Value1",
Prop2= 123
},
new SampleModel() {
Prop1= "Value2",
Prop2= 456
}
};
}
}
Returning the response:
[
{"Prop1": "Value1", "Prop2": 123},
{"Prop1": "Value2", "Prop2": 456}
]