The API controller is essential for communication between the server and Flexmonster Pivot. This guide describes how to implement the API controller for Flexmonster.DataServer.Core.dll
.
Note This tutorial relates to the guide Referencing the Data Server as a DLL and should be completed as a part of it.
Step 1.1. If needed, create the Controllers/
folder inside the project:
Step 1.2. Then, create an empty FlexmonsterAPIController.cs
file inside the Controllers/
folder.
Step 1.3. Define the "api"
route using the [Route]
attribute and ensure the FlexmonsterAPIController
extends the ControllerBase class:
using Microsoft.AspNetCore.Mvc;
[Route("api")]
[ApiController]
public class FlexmonsterAPIController : ControllerBase
{
}
To use the DLL in the controller, add the following line of code to the beginning of the FlexmonsterAPIController.cs
file:
using Flexmonster.DataServer.Core;
IApiService
is a class from Flexmonster.DataServer.Core.dll
that contains methods for handling custom data source API requests. These methods enable getting fields, members, and aggregated data.
To use these methods, add the field of the IApiService
type (e.g., _flexmonsterApiService
) to the controller class and initialize the field in the controller's constructor:
private readonly IApiService _flexmonsterApiService;
public FlexmonsterAPIController(IApiService apiService)
{
_flexmonsterApiService = apiService;
}
The Flexmonster.DataServer.Core.Models
class contains type definitions for requests and responses. To include it in the controller, add the following line of code to the beginning of the FlexmonsterAPIController.cs
file:
using Flexmonster.DataServer.Core.Models;
Flexmonster sends the /handshake request to establish communication with the server. The component includes its version in the request and expects a response with the custom data source API version implemented by the server.
In the response to the /handshake request, it is recommended to send the flexmonster.js
version used at the time of implementing the custom data source API. For example, if you handle the request while using Flexmonster Pivot version 2.9.80, specify 2.9.80
as the server’s custom data source API version.
Here is an example of how to handle the /handshake request:
// Specify the version of the custom data source API (e.g., 2.9.80)
const string API_VERSION = "<server_API_version>";
[Route("handshake")]
[HttpPost]
public IActionResult Handshake()
{
object response = new { version = API_VERSION };
return new JsonResult(response);
}
The next type of request that must be handled is the /fields request. To handle it, use the GetFields() method of the IApiService
class:
[Route("fields")] [HttpPost] public IActionResult PostFields([FromBody]FieldsRequest request) { var response = _flexmonsterApiService.GetFields(request); return new JsonResult(response); }
FieldsRequest
is a predefined class from Flexmonster.DataServer.Core.Models
. It describes the /fields request's structure.
The next required request to handle is the /members request. It should be handled with the GetMembers() method of the IApiService
class. This can be done as follows:
[Route("members")] [HttpPost] public IActionResult PostMembers([FromBody]MembersRequest request) { var response = _flexmonsterApiService.GetMembers(request); return new JsonResult(response); }
MembersRequest
is a predefined class from Flexmonster.DataServer.Core.Models
. It describes the /members request's structure.
The last requests that must be handled are the /select requests for the pivot table, the flat table, and the drill-through view. To handle them, use the GetAggregatedData() method of the IApiService
class:
[Route("select")] [HttpPost] public IActionResult PostSelect([FromBody]SelectRequest request) { var response = _flexmonsterApiService.GetAggregatedData(request); return new JsonResult(response); }
SelectRequest
is a class from Flexmonster.DataServer.Core.Models
. It describes the structure of the /select requests.
After handling all the requests, the controller is ready for use. Now you can return to the Referencing Flexmonster Data Server as a DLL guide and complete it.
Here's an example of the FlexmonsterAPIController.cs
with a fully working code:
using Microsoft.AspNetCore.Mvc;
using Flexmonster.DataServer.Core;
using Flexmonster.DataServer.Core.Models;
[Route("api")]
[ApiController]
public class FlexmonsterAPIController : ControllerBase
{
private readonly IApiService _flexmonsterApiService;
public FlexmonsterAPIController(IApiService apiService)
{
_flexmonsterApiService = apiService;
}
const string API_VERSION = "2.9.80";
[Route("handshake")]
[HttpPost]
public IActionResult Handshake()
{
object response = new { version = API_VERSION };
return new JsonResult(response);
}
[Route("fields")]
[HttpPost]
public IActionResult PostFields([FromBody]FieldsRequest request)
{
var response = _flexmonsterApiService.GetFields(request);
return new JsonResult(response);
}
[Route("members")]
[HttpPost]
public IActionResult PostMembers([FromBody]MembersRequest request)
{
var response = _flexmonsterApiService.GetMembers(request);
return new JsonResult(response);
}
[Route("select")]
[HttpPost]
public IActionResult PostSelect([FromBody]SelectRequest request)
{
var response = _flexmonsterApiService.GetAggregatedData(request);
return new JsonResult(response);
}
}
You may be interested in the following articles: