When creating a RegisterTypedRequestFilter I am trying to terminate the processing of the request whilst including a Response DTO - is this doable?
I am trying to set the res.Dto property then calling req.EndRequest, but the result is empty content. I assumed that since there is a method called EndRequestWithNoContent, that the plain EndRequest would include content (the dto), but it looks like I am missing a step.
Any thoughts?
appHost.RegisterTypedRequestFilter<Foo>((req, res, dto) =>
{
// determine if request should be bypassed or whatnot
res.Dto = new FooResponse();
res.EndRequest();
});
You need to write what you want directly to the response, EndRequest() is how to short-circuit the Request pipeline, whilst EndRequestWithNoContent() sends back a 204 NoContent response with a 0 Content-Length:
You can use WriteToResponse() but as that’s async you should only do that within an Async Request Filter:
GlobalRequestFiltersAsync.Add(async (req, res, dto) => {
if (dto is Foo foo)
{
await res.WriteToResponse(req, new FooResponse());
return;
}
});
If you’re not using .NET Core you can write to the response OutputStream synchronously, e.g.
Fantastic. I guess what threw me off was that I had a public setter on the response dto giving me the impression that it would be set, then terminate the request. But writing to the output stream or response should be fine.