Skip to content

Handlers

Andrew Bullock edited this page Oct 30, 2017 · 3 revisions

Handler Overview

Handlers are the equivalent to MVC Controllers, except they (typically) only handle a single URL, rather than containing many MVC 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. As with most classes, they should be sealed and internal unless otherwise required.

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 UD.Core.Web.ViewEngines.Razor.RazorViewLocator for more on how .cshtmls are found. Note that RazorViewLocator still supports the now deprecated mechanism of searching for culture specific .cshtml filenames. This should no longer be used.

TODO

Clone this wiki locally