Returning arbitrary HTML from XData server

This ticket may actually belong under Sparkle, especially if the right way to do this is from Middleware, but basically I want to watch for some conditions on all requests coming to my XData server (possibly before it's dispatched to XData) and return arbitrary data back: setting code, ContentType, content, etc. (hopefully ContentLength would be auto-calculated, but maybe setting it too, if not).

What is the easiest way to do this?

I first thought that it should be done at XData level, but then I would have to add extra code to every exposed method, then I remembered the Middlware and it looks like it could be possible with Generic one, without even subclassing it. But when I tried simply doing Context.Response.Content.WriteData('

Test Page

'), it did not work: ContentLength was 0, etc.

The middleware is indeed the best place to do this if it will apply to all requests coming from the client.

You can indeed simply use Context.Response and set all headers, content body and status code, as explained here:

For a simple response, it's easier to use the Close method:

    C.Response.StatusCode := 200;
    C.Response.ContentType := 'text/plain';
    C.Response.Close(TEncoding.UTF8.GetBytes('Test Page'));