Model binding for multipart / form-data in WebApi

I have a 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; }
}

      

The api needs to get some statistics with binary data from clients with multipart / formdata. There are several similar methods with different models, so:

  • I don't want to write my own MediaTypeFormatter for each model
  • I want to keep the default Binding Model for simple fields, due to validation and simple binding
  • We have a helper api generator like this . So it is bad to parse a request in a controller in any situation.

Want to bind in mvc style :)

[HttpPost]
public HttpResponseMessage SiteMark(Model model)
{

      

I read this solution Web API model binding not working with HttpPostedFileBase? but it doesn't seem very suitable for my situation and doesn't solve the problem of simple field bindings.

I am thinking of writing a custom ModelBinder that uses the standard binding for simple fields. Even we can use the default mvc binder for this (the default webapi binding is apparently not achievable in native code). Then read all binary data in a loop with custom code and map it to binary fields.

Perhaps I am missing something and there is an easier solution?

+3


source to share





All Articles