0

Is it possible (or sensible) to build a custom MediaTypeMapping to select a MediaTypeFormatter based on content from the Request Body?

I'm thinking specifically of a case where I have a very non-descriptive text/plain in a multipart/form-data payload and I want to use heuristics to work out what's contained.

I'm currently doing this in a custom MediaTypeFormatter that looks at the first few lines, and if the content begins with { then it assumes JSON, if it's word,word,word the the commas indicate CSV.

I was thinking that a MediaTypeMapping would be cleaner but I don't know if it's sensible to try and match from the Body content.

cirrus
  • 5,624
  • 8
  • 44
  • 62
  • Another useful case for this would be a MultipartMediaTypeFormatter that could process an upload and then look for existing formatters to bind the various parts. http://stackoverflow.com/questions/12697310/web-api-model-binder-doesnt-work-with-httppostedfilebase and http://stackoverflow.com/questions/12593001/web-api-model-binding-with-multipart-formdata/12603828#12603828 – cirrus Feb 18 '13 at 14:01

1 Answers1

0

Currently there is no way you can really Inspect content in Webapi. You can not read the stream more than once.

Tabish Sarwar
  • 1,505
  • 1
  • 11
  • 18
  • I don't want to read the stream more than once...I just want to peek ahead :) In any case, I'm not sure how much I can do with a single multipart/form-data payload reader because I can't return more than one type in any case. Nor can I bind more than one complex class (read from the body) to an Action. It's a shame there's no multipart form reader out of the box that binds multiple complex model types. – cirrus Feb 18 '13 at 18:30
  • Well the i think you should Stream.Seek(0, SeekOrigin.Begin) when you are done with the Stream . Tell me if this works out. – Tabish Sarwar Feb 18 '13 at 18:51