Generally speaking, this strikes me as a lot of work to be doing in a web request handler. Have you considered that it might be more appropriate for this to live somewhere else, like in a manager? That way the creation of this data structure wouldn't be married to the HTTP request/response workflow.