Integrating .NET Controllers with Titan

Author:Ben Schulz
Last Updated:November 01, 2022 3:59 PM

Controller classes can be integrated with Titan relatively easily. There are four main steps:

  1. Add the controller to the SDK
  2. Add the necessary packages
  3. Configure the controller appropriately
  4. Configure the front end appropriately

The controller can be placed anywhere in the SDK but for clarity’s sake it is best to put it near other code interacting with your view. In this example, a controller has been created to handle incoming POSTs from a data detail block (this directory is also a good location to store any custom JavaScript created for this block).

Before work can be done on the controller you will need to add the Microsoft.AspNet.Mvc package to the UISupport project. Use Nuget to add a reference to version 5.2.7 of this package (this will automatically install a few other packages as well but you do not need to worry about these). Once the package is installed you may notice a new “App_Start” folder in your Solution Explorer that contains various Config class files. If it is there, delete it.

Open the new controller. The bracketed lines, known as Data Annotations, are used to define properties for classes, methods, and even variables, though the latter is outside the scope of this guide. The following annotations need to be added:

  1. The controller needs to inherit the Controller class.
  2. The controller needs a [RoutePrefix]. This defines the URL root that will be used for any and all HTTP actions.
  3. All methods need to have a [Route]. This further defines the URL the front-end will need to use.
  4. All methods need to have a HTTP method type. This guide will only cover [HttpPost], but there are many other types.

Back on the front end, add the above ajax call to the JavaScript method that you want to access the controller. Note that the url field is the concatenation of the RoutePrefix and Route attributes from the controller. Inside the data field, match the name of any parameters in the controller with the values you want to send to the controller. In this example, the string “value” will be posted, which corresponds to PostMethod’s signature. You can add any number of parameter/value pairs to the data field as long as the names match what the controller expects.

Add a breakpoint to the controller method (see the documentation on local debugging for more details on how to do this). The Ajax should trigger this breakpoint as soon as it executes. Here, you can see that “value” was passed in. This method will send the string “value was passed to this method” back to the front end. There are many ways for an Ajax call to interpret and work with data that has been returned to it from a controller, and documentation for this is widely available elsewhere on the internet.

top