Did you know that ASP.NET Web API allows you to batch requests? Batching packs several API requests together and send them in one HTTP request. The responses will be grouped in one single HTTP response as well.
There isn’t much you need to do to configure this on the server, just add a specific route to your route table with batching enabled:
The important things to notice are the following:
- The DefaultHttpBatchHandler executes each request sequentially, but in case you don’t have any dependency between requests, you can set a property to make execution non-sequential.
- The MapHttpBatchRoute route template can contain parameters. So, in case you create your own batch handler, you can pass in parameters to it.
If you don’t need the sequential behavior you can create your own HttpBatchHandler and override the execution order property:
That’s all that is required to enable batching on the server. Let’s now see how to use this on the client:
This is a little known feature but can have a big impact on the performance of your web api. Use it wisely