4

I have model like this:

public class Model
{
        [MaxLength(500)]
        public string UserEmail { get; set; }

        [Required]
        [MaxLength(100)]
        public string Product { get; set; }

        // ... And other 10 parameters

        public byte[] StatisticsData { get; set; }

        public byte[] UserXmlFile { get; set; }
}

Api should get some statistics information with binary data from clients with multipart/formdata. There are several similar methods with different models, so:

  1. I don't want to write custom MediaTypeFormatter for each model
  2. I want to keep standart Binding Model to simple fields, because of validation and easy binding
  3. We have an api help generator like this . So it's bad to parse the request in the controller in every situation.

Want to bind in mvc style :)

[HttpPost]
public HttpResponseMessage SiteMark(Model model)
{

I read this solution Web API model binder doesn't work with HttpPostedFileBase?, but it seems not very suitable for my situation and doesn't solve the problem of simple fields bindings.

I think about writing custom ModelBinder that uses standart binding for simple fields. Even we can use default mvc binder for this (webapi default binding seems to be unreusable in own code). And then Read all binary data in the cycle by custom code and map it to binary fields.

Maybe I'm missing something and there is a simpler solution?

Community
  • 1
  • 1
Alexander Subbotin
  • 911
  • 1
  • 7
  • 5
  • I don't think this link will help directly with your problem but it does cover some very interesting points and gotchas when uploading files to Web API which you might find interesting! http://www.strathweb.com/2012/09/dealing-with-large-files-in-asp-net-web-api/ – Luke Baughan Feb 14 '13 at 10:06
  • Here's a stack post which might address some of the problem http://stackoverflow.com/questions/12593001/web-api-model-binding-with-multipart-formdata – Luke Baughan Feb 14 '13 at 10:08
  • Think a custom MediaTypeFormatter would be the way to go. You dont have to write one for each model. – Matt Randle Feb 14 '13 at 10:41
  • I have read this post, but they have situations where this solution works http://lonetechie.com/2012/09/23/web-api-generic-mediatypeformatter-for-file-upload/ Thanks for the first link, its interesting, but in my case we usually receive not large binnary data. – Alexander Subbotin Feb 14 '13 at 10:56
  • @MattRandle In my opinion, there is not much difference writing custom ModelBinder or MediaTypeFormatter for this situation. You're right that i can write one formatter for all models. – Alexander Subbotin Feb 14 '13 at 13:26
  • 4
    Did u ever come up with a solution for thid. We are trying to do the same type of thing si any help would be great. Thanks – Jonnymaboy Jan 09 '15 at 09:16

0 Answers0