Skip to content
This repository was archived by the owner on Oct 27, 2020. It is now read-only.

Controllers

Trevor Pilley edited this page Mar 16, 2020 · 8 revisions

The easiest way to get started is to inherit your controller from the MicroLiteController.

This will give you a Session property which is an ISession.

public class CustomerController : MicroLiteController
{
}

(There is also a MicroLiteReadOnlyController if you only want access to an IReadOnlySession).

You can then use the Session to communicate with the database (the example below is based upon all action filters being registered - hence the lack of null checks & model state validation code):

[HttpGet]
public async Task<ActionResult> Edit(int id)
{
    var customer = await this.Session.SingleAsync<Customer>(id);

    if (customer == null)
    {
        return this.HttpNotFound();
    }

    return this.View(model);
}
[HttpPost]
public async Task<ActionResult> Edit(Customer customer)
{
    await this.Session.UpdateAsync(customer);

    return this.RedirectToAction("Index", new { Id = customer.Id });
}

Clone this wiki locally