Custom .NET Controller
UPDATED: | POSTED: | by Serkan Munar
In this article, we will demonstrate how to extend theInstall the NuGet PackageM.App Enterprise Server
with a custom C#.NET Controller. Additionally, we will show how to protect the controller from external access usingAPI Key Authentication.
Microsoft.AspNet.WebApi.Core
for your project.
MApp.Code.dll
and MApp.Endpoints.dll
. You can find these in the bin
directory in the installation folder of M.App Enterprise. We recommend copying the dll files to your own project directory first and creating the reference from there.
Create a C# class file and add following sample code, which contains a simple controller and an [HttpGet]
action.
using MApp.Endpoints;
using MApp.Core;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Web.Http;
using MApp.Data;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace CustomControllers
{
[MAppAuthentication(AllowedClients.ApiKey)]
public class CustomController : MAppTenantController
{
[HttpGet]
[Route("api/v1/custom/action1")]
public async Task<IHttpActionResult> Action1()
{
//Fetch and process data
var responseObject = new
{
message = "Hello World!"
};
return this.JsonData(JsonConvert.SerializeObject(responseObject));
}
}
}
Now you have a custom controller which extends our basic controller MAppTenantController
. In this example we also added an ApiKey authentication
attribute, which limits the public access to the actions of this controller. So calling this action is only allowed with a valid access token.
Studio
→ Security
→ API Keys
Furthermore, it would also be possible to allow access only for requests from M.App Enterprise Apps. To do this, you need to change the authorization attribute to [MAppAuthentication(AllowedClients.App)]
only.
The [Route("...")]
attribute defines the public url for this action. In our case the http-get will be available at http://{YOUR MAE WEB PATH}/api/v1/custom/action1
.
By extending from our basic controller MAppTenantController
you get access to various functions and properties provided by M.App Enterprise Server.
Selecting the Database Connections stored in your M.App Enterprise environment.
List<Connection> connections = (await GetTenant().Database.SelectAsync<Connection>()).ToList();
Executing a SQL query command.
Database database = new Database({ConnectionString}, DbProviderType.PostgreSql);
database.ExecuteNonQuery("UPDATE mytable SET column1 = 'value1'");
Build your Visual Studio solution and copy the compiled .dll
files to your M.App Enterprise installation directory and paste the files into the bin
folder
After placing the .dll
files into the bin
folder of M.App Enterprise, your controller actions become available to be called.
As an example, you can call a function to fetch the data in your app:
//custom script placed in M.App Enterprise AppEngine
function fetchData() {
fetch('../api/v1/custom/action1', {
headers: {
'Tenant': {TENANT},
'Authorization': `ApiKey {ACCESS TOKEN}`
}
})
.then(response => {
debugger;
return response.json();
})
.then(data => {
debugger;
console.log(data);
});
}
fetchData();
{TENANT}
with your tenant id and {ACCESS TOKEN}
with your ApiKey token generated in M.App Enterprise Studio.