Skip to content
This repository has been archived by the owner on Jun 30, 2021. It is now read-only.

Controllers basics

Alexanderius edited this page Jun 23, 2019 · 5 revisions

Controllers basics

Simplify.Web has 4 types of controllers base classes:

  • Controller - general controller, processes HTTP request;
  • AsyncController - the same as Controller class, but can process request asynchronously (can contain await operations and return Task);
  • Controller<T> - the same as Controller class, but can get view model from HTTP request;
  • T - is a type of view model;
  • View model can be accessed via controller T Model property;
  • View model will be parsed from request only on T Model first access.
  • AsyncController<T> - the same as Controller<T> but can process request asynchronously.

The controller structure

  • User created controller must be derived from one of the controller base classes specified above;
  • Should contain Invoke method (executes on request process);
  • Should return instance of any class derived from ControllerResponse class or return null;
  • 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 controller does not have any attributes, then it will run on each HTTP request;
  • To set controller for handling only 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.

Example of a controller which serializes 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 new Json(items);
        }
        catch (Exception e)
        {
            Console.WriteLine(e);

            return StatusCode(500);
        }
    }
}

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

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

Example of a 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()
    {
        return new StaticTpl("Static/About");
    }
}

Async controllers

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

public class NavbarController : AsyncController
{
    public async override Task<ControllerResponse> Invoke()
    {
        return new InlineTpl("Navbar", await TemplateFactory.LoadAsync("Navbar"));
    }
}
  1. After calling await TemplateFactory.LoadAsync("Navbar") method execution will be passed to an another controller (which also can handle current request) and they will be executed in parallel;
  2. At the end of the execution of all controllers for the current request controller responses will be processed for each async controller (sync controllers responses is processed immediately after Invoke method finishes its execution).

Accessing a views

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