Skip to content

Handlers

Andrew Bullock edited this page Jun 2, 2018 · 3 revisions

Handler Overview

Handlers are the equivalent to ASP.NET MVC Controllers, except they (typically) only handle a single URL, rather than containing many Actions for many URLs.

Handlers must implement IHandler, although you almost certainly want to implement the abstract Handler class, as it provides helpful base methods for resolving Views and other typical Handler responses.

Implementations of IHandler are automatically discovered and wired up.

sealed class ExampleHandler : Handler
{
    public IResult Get()
    {
         return View("ViewName");
    }
}

Handling HTTP Methods

Handlers respond to (handle) requests by having public methods on them, named in accordance with the HTTP Method they handle. These methods must be public and return an IResult or Task<IResult>.

public async Task<IResult> Get()
{
    var thing = await this.thingRepository.GetThing();
    return View("ViewThing", thing);
}

View Resolution

This will search for a view called foo.cshtml within the same namespace/folder as the Handler:

public IResult Get()
{
     return View("foo");
}

See Views for more on advanced view functionality

TODO

Clone this wiki locally