Adding/skipping middleware filters

May 20, 2015 1:02 pm Published by Leave your thoughts

Sometimes for external HTTP requests (internal Self requests are not filtered) one would like to have some request pre-processing or simply filter-out certain requests completely. For this purpose we introduced so-called “middleware or request filters”. Its basically a list of user-supplied delegates that are executed for external requests before the actual handlers are called. Each filter in the list is executed one by one, until some filter returns a non-null Response. If Response object was created in request filter then its returned to the client and the actual handler will not be called. If none of the filters returned a response object then execution proceeds to the actual handler.

Here is the function to add a middleware filter to the list:

// Register a request filter
Application.Current.Use(request => {
    return null;

Sometimes one would however like to completely skip all request filters and process the external request “as-is”. For this, when declaring handler, user has to supply HandlerOptions:

Handle.GET("/my-url", () => new Page(), new HandlerOptions() { SkipRequestFilters = true });

There are various applications for middleware filters: security, logging, wrapping, request modification, etc.
One notable example is a Launcher application that wraps responses from all running applications into its own response.

Here is the link to documentation: Using middleware

Categorised in:

This post was written by Alexey Moiseenko

Leave a Reply

Your email address will not be published. Required fields are marked *