Skip to content

Controllers responses

Alexanderius edited this page Jun 6, 2024 · 3 revisions

Controllers responses

All default controllers responses can be found in Simplify.Web.Responses namespace.

StaticTpl

Loads specified template and puts it to the data collector (template data will be added to the MainContent variable of the DataCollector).

  • StaticTpl(string templateFileName) - just loads and puts template data to the DataCollector.
public override ControllerResponse Invoke()
{
    return StaticTpl("Default");
}
  • StaticTpl(string templateFileName, string title) - the same as above, but also adds a site title into the Title variable of DataCollector.
public override ControllerResponse Invoke()
{
    return StaticTpl("MyPageTemplate", StringTable.MyPageTitle);
}

Tpl

Puts a string into the MainContent variable of the DataCollector.

public override ControllerResponse Invoke()
{
    return Tpl("Some text");
}
public override ControllerResponse Invoke()
{
    return Tpl("Some text", StringTable.MyPageTitle);
}
public override ControllerResponse Invoke()
{
    return Tpl(TemplateFactory.Load("MyPageTemplate").Get(), StringTable.MyPageTitle);
}

InlineTpl

The same as Tpl, but at first parameter you can specify exact DataCollector variable name.

public override ControllerResponse Invoke()
{
    return InlineTpl("LoginControl", TemplateFactory.Load("LoginControlTemplate").Get());
}

Redirect

Redirects the client to an URL or by specifying a redirection type.

public override ControllerResponse Invoke()
{
    return Redirect("http://somelink.com");
}
public override ControllerResponse Invoke()
{
    return Redirect(RedirectionType.PreviousPage);
}

Content

Sends a string to the client and HTML page will not be generated.

public override ControllerResponse Invoke()
{
    return Content("Some string");
}

StatusCode

Same as Content response, just semantically different. You can clearly specify the HTTP response status code with optional response.

public override ControllerResponse Invoke()
{
    return StatusCode(204);
}

NoContent

Same as StatusCode with HTTP status code equals to 204.

public override ControllerResponse Invoke()
{
    return NoContent();
}

Json

Sends a JSON to the client and HTML page will not be generated.

public override ControllerResponse Invoke()
{
    return Json();
}

File

Sends a file to the client

public override ControllerResponse Invoke()
{
    return new File("MyFile.txt", "text/plain", Encoding.UTF8.GetBytes("My file content"));
}

ViewModel

Serializes a view model and puts it into a template, then that template will be put into the MainContent variable of the DataCollector.

public class LoginViewModel
{
    [Required]
    public string UserName { get; set; }

    [Required]
    public string Password { get; set; }

    public bool RememberMe { get; set; }
}

public override ControllerResponse Invoke()
{
    var model = new LoginViewModel {UserName = "Foo"};
    return new ViewModel<LoginViewModel>("LoginPage", model);
}

Json

Json response live in it's own repository

Aliases

For each response there is a method in controllers base class with the same name as the name of the controller response class, so you don't need to specify new keyword for built-in controller responses.

Example

public override ControllerResponse Invoke()
{
    return StatusCode(204);
}

<< Previous page Next page >>

Clone this wiki locally