We have updated Flexmonster Software License Agreement, effective as of September 30, 2024. Learn more about what’s changed.
All documentation
  • Introduction
  • Connecting to data source
    1. Supported data sources
    2. Connecting to other data sources
  • Browser compatibility
  • Documentation for older versions
  • Implementing the API controller

    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. Create a new API controller

    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
    {
    }

    Step 2. Include Flexmonster.DataServer.Core in the controller

    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;

    Step 3. Add the IApiService field to the controller

    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;
    }

    Step 4. Include Flexmonster.DataServer.Core.Models in the controller

    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;

    Step 5. Handle the /handshake request

    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);
    }

    Step 6. Handle the /fields request

    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.

    Step 7. Handle the /members request

    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.

    Step 8. Handle the /select requests

    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.

    Check out the full code

    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);
    }
    }

    What's next?

    You may be interested in the following articles: