Skip to content

Controller basics

Alexanderius edited this page Jul 3, 2025 · 1 revision

Controller basics

Simplify.Web has 6 types of controller base classes:

Version 2 controllers

  • Controller2 - general controller, processes HTTP requests;
  • Controller2<T> - same as Controller2 class, but can get a view model from HTTP request (T is the type of view model);

Version 1 controllers

  • Controller - general controller, processes HTTP requests;
  • AsyncController - same as Controller class, but can process requests asynchronously (can contain await operations and return Task);
  • Controller<T> - same as Controller class, but can get a view model from HTTP request (T is the type of view model);
  • AsyncController<T> - same as Controller<T> but can process requests asynchronously.

The common controller structure for v1 and v2

  • User-created controller must be derived from one of the controller base classes specified above;
  • Should contain an Invoke method (executes on request process);
  • Classes derived from ControllerResponse can do some actions, for example:
    • Put template data to DataCollector (main class for page HTML data collection);
    • Return some JSON data using Json response;
    • Redirect client to another page.
  • If a controller does not have any attributes, then it will run on each HTTP request;
  • To set a controller for handling only a specific action and request type, you can use controller attributes from Simplify.Web.Attributes namespace.

Here is the list, description, and examples of available controller responses.

Here is the list, description, and examples of available controller attributes.

The specific controller v1 structure

  • Should return an instance of any class derived from ControllerResponse class or return null if there is no controller response to return;

The specific controller v2 structure

  • Can have a return type of void, Task, ControllerResponse, or Task<ControllerResponse>;
  • If it has void return type, then it means the controller is synchronous and doesn't need to return a controller response;
  • If it has Task return type, then it means the controller is asynchronous and doesn't need to return a controller response;
  • If it has ControllerResponse return type, then it means the controller wants to return a controller response and it is synchronous;
  • If it has Task<ControllerResponse> return type, then it means the controller wants to return a controller response and it is asynchronous;

V2 controllers examples

Example of the controller which serializes a collection to a JSON string thereby skipping backend page generation

[Get("api/weatherTypes")]
public class SampleDataController : Controller2
{
    private static readonly string[] Summaries =
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    public ControllerResponse Invoke()
    {
        try
        {
            return Json(items);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);

            return StatusCode(500);
        }
    }
}

Example of the controller which deletes a user via the users service

[Delete("api/v1/users/{id}")]
public class SampleDataController(IUsersService service) : Controller2
{
    public async Task<ControllerResponse> Invoke(int id)
    {
        await service.DeleteAsync(id);

        return StatusCode(204);
    }
}

Example of the controller which loads a Navbar.tpl template file and puts it to the DataCollector Navbar variable

public class NavbarController : Controller2
{
    public ControllerResponse Invoke() =>
        InlineTpl("Navbar", TemplateFactory.Load("Navbar"));
}

V1 controllers examples

Example of the controller which serializes a collection to a JSON string thereby skipping backend page generation

[Get("api/weatherTypes")]
public class SampleDataController : Controller
{
    private static readonly string[] Summaries =
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    public override ControllerResponse Invoke()
    {
        try
        {
            return Json(items);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);

            return StatusCode(500);
        }
    }
}

Example of the controller which loads a Navbar.tpl template file and puts it to the DataCollector Navbar variable

public class NavbarController : Controller
{
    public override ControllerResponse Invoke() =>
        InlineTpl("Navbar", TemplateFactory.Load("Navbar"));
}

Example of the v1 controller which loads an About.tpl template file and puts it to the DataCollector MainContent variable

This controller runs only on HTTP GET request with about action like: http://mysite.com/about

[Get("about")]
public class AboutController : Controller
{
    public override ControllerResponse Invoke() =>
        StaticTpl("Static/About");
}

Example of v1 async controller

Async controllers must return Task<ControllerResponse> instead of just ControllerResponse

public class NavbarController : AsyncController
{
    public async override Task<ControllerResponse> Invoke() =>
        InlineTpl("Navbar", await TemplateFactory.LoadAsync("Navbar"));
}

Accessing a view

To access a view from a controller you should use GetView<T>() method.

public class MyController : Controller
{
    public override ControllerResponse Invoke()
    {
        var view = GetView<LoginView>();
        ...
    }
}

<< Previous page Next page >>

Clone this wiki locally