Skip to content

Custom middleware, adding JSON parser #117

Answered by scottoffen
AgrYpn1a asked this question in Q&A
Discussion options

You must be logged in to vote

Yes.

You can parse the JSON in a single place for all your route using Router.BeforeRoutingAsync. These handlers are delegates of type RoutingAsyncEventHandler, and run just before any routes are executed (and there is a corresponding router.AfterRoutingAsync). This is where you want add your middleware.

In your middleware, after parsing the body, you can add the json to the IHttpRequest.Locals property, a key-value store that is used to pass state between all routes for a single request.

Here is one sample of how to do this:

public static class IRestServerExtensions
{
    public static void AutoParseJson(this IRestServer server)
    {
        server.Router.BeforeRoutingAsync += ParseJson…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@scottoffen
Comment options

@AgrYpn1a
Comment options

@scottoffen
Comment options

@AgrYpn1a
Comment options

Answer selected by AgrYpn1a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #116 on March 16, 2022 15:36.