Controller
The Controller is the base class for REST API controllers
Every controller must inherit from Controller. Public instance methods decorated with a Route attribute can then be registered by MapController<T>() or MapControllers<T>().
Lifecycle and construction
By default, SimpleW creates a new controller instance for every handler execution.
In most cases, no constructor needs to be declared:
public class UserController : Controller {
[Route("GET", "/users")]
public object List() {
return new[] { "Alice", "Bob" };
}
}C# automatically provides a public parameterless constructor when the class does not declare any constructor.
For constructor injection, use the Dependency Injection helper, which replaces the default controller action executor factory. A controller instance is scoped to one handler execution. Do not use controller instance fields to store application-wide or cross-request state.
Session
/// <summary>
/// Gets the current HTTP Session
/// </summary>
public HttpSession Session;This property contains the current Session instance. See the HttpSession class for uses.
Request
/// <summary>
/// Gets the current HTTP Request (alias to Session.Request)
/// </summary>
public HttpRequest Request;This property contains all the information about the request. See the HttpRequest class for uses.
Response
/// <summary>
/// Gets the current prepared HTTP Response (alias to Session.Response)
/// </summary>
public HttpResponse Response;This property can be used to return a response object that will be sent to the client. It's initialized as an empty Response instance. See the HttpResponse class for uses.
Principal
/// <summary>
/// Gets the current resolved user.
/// </summary>
public HttpPrincipal Principal => Session.Principal;This property can be used to define security. See the HttpPrincipal class for uses.
Bag
/// <summary>
/// Gets the current HttpBag
/// </summary>
public HttpBag Bag => Session.Bag;This property can be used to pass data between middleware. See the HttpBag class for uses.
Metadata
/// <summary>
/// Gets the metadata attached to the matched handler.
/// </summary>
public HandlerMetadataCollection MetadataSee the HandlerMetadataCollection class for uses.
OnBeforeMethod
This callback is defined as
/// <summary>
/// Override this Handler to call code before any Controller.Method()
/// </summary>
public virtual void OnBeforeMethod() { }