-
Notifications
You must be signed in to change notification settings - Fork 9
Controllers basics
AcspNet have 4 types of controllers base classes:
-
Controller- general controller, processes HTTP request; -
AsyncController- the same asControllerclass, but can process request asynchronously (can contain await operations and return Task); -
Controller<T>- the same asControllerclass, but can get view model from request.T- is a type of view model. View model can be accessed via this controllerT Modelproperty. View model will be parsed from request only onT Modelfirst access; -
AsyncController<T>- the same asController<T>but can process request asynchronously.
Each user created controller must be derived from one of this controller classes and contain Invoke method.
This method will be called on request process.
Each controller must return instance of some class derived from ControllerResponse type or null.
Classes derived from ControllerResponse can do some actions, for example, to put template data to DataCollector (main class for page HTML data collection) or to redirect client to another page.
Here is the list, description and examples of available controller responses.
Example of controller which loads Navbar.tpl template file and puts it to DataCollector Navbar variable.
public class NavbarController :Controller
{
public override ControllerResponse Invoke()
{
return new InlineTpl("Navbar", TemplateFactory.Load("Navbar"));
}
}If controller does not have any attributes, then it will run on each HTTP reqest.
To set controller for handling only specific action and request type you can use controller attributes from AcspNet.Attributes namespace.
Example of controller which loads About.tpl template file and puts it to 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");
}
}Here is the list, description and examples of available controller attributes.
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"));
}
}Then, after calling await TemplateFactory.LoadAsync("Navbar") method execution will be passed to another controller which also can handle current request, and them will be executed in paraller.
At the end of execution of all controllers for current request, controller responses will be processed for each async controller (sync controllers responses is processed immediately after Invoke method finished its execution).
To access a view from controller you should use GetView<T>() method.
public class MyController : Controller
{
public override ControllerResponse Invoke()
{
var view = GetView<LoginView>();
...
}
}- Getting Started
- Main Simplify.Web principles
- Simplify.Web controllers
- Simplify.Web views
- Simplify.Web templates
- Simplify.Web configuration
- Templates variables
- Static content
- Template factory
- Data collector
- String table
- File reader
- Web context
- Environment
- Language manager
- Redirector
- HTML