Skip to content

Model Binding

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

Model Binding

Unlike ASP.NET MVC, a Handler method will never fail to execute because of a binding failure. Handler method parameters will be defaulted if they cannot be bound. Given this handler:

[Handle("/some/path/{boolean}")]
sealed class ExampleHandler : Handler
{
    public IResult Get(bool boolean)
    {
        // ...
    }
}

Visiting /some/path/true results in boolean being set to true.

Visiting /some/path/foo results in boolean being defaulted to false.

When parameters fail to bind, the error is reported through the IRequest.State property (similar to ASP.NET MVC's ModelState).

To access the Request's State, take an IRequest parameter, or access it through this.Context.Request.State:

public IResult Get(IRequest request, bool boolean)
{
    if(!request.State.IsValid)
    {
        // we failed to bind something (or validate)
        // request.State.Errors contains what went wrong
    }
}

For more on how the binders work, see MustardBlack.Handlers.Binding

TODO

Clone this wiki locally