title: .NET 6 Core C# -building a rest webservice with binary multipart/form-data upload returning json
body: <p>I'm trying to build a .NET 6.0 C# rest webservice, receiving a string and three files, one of them a binary image.
  The files are not saved, just checked/evaluated and the result is returned in a json structure.
  The string can be an url path parameter.
  The webservice should run on windows and in a linux docker container.
  It is called only from C# clients and is not accessible from the internet.</p>
  <p>So the curl looks like that:</p>
  <pre><code>curl -X POST -H &quot;Content-Type: multipart/form-data; boundary=------------------------d74496d66958873e&quot; \
  --data-binary &quot;@KeyValue.json&quot; \
  --data-binary &quot;@Text.txt&quot; \
  --data-binary &quot;@Image.tif&quot; \
  http://localhost:5000/check/CheckType01
  </code></pre>
  <p>I tried an approach starting with the Visual Studio 2022 Project &quot;ASP.NET Core-Web-Api&quot;</p>
  <pre><code>var builder = WebApplication.CreateBuilder(args);
  var app = builder.Build();
  
  app.MapPost(&quot;/check/{checkType}&quot;, (string checkType, HttpContext ctx) =&gt;
  {
      ctx.Request.EnableBuffering();
      ctx.Request.Body.Position = 0;
      var reader = new StreamReader(ctx.Request.Body);
      var body = Task.Run(async () =&gt; await reader.ReadToEndAsync().ConfigureAwait(false));
  
      Console.WriteLine(&quot;Parameter:  &quot; + checkType);
      Console.WriteLine(&quot;MultiPart:  &quot; + body.Result);
  
      return new { check = &quot;Ok&quot;, confidence = 100 }; ;
  });
  
  app.Run();
  </code></pre>
  <p>With the three files containing &quot;KeyValueContent&quot;, &quot;TextContent&quot; and &quot;ImageContent&quot;, this code partially works:</p>
  <pre><code>Output:
  Parameter:  CheckType01
  MultiPart:  KeyValueContent&amp;TextContent&amp;ImageContent
  Return:
  {&quot;check&quot;:&quot;Ok&quot;,&quot;confidence&quot;:100}
  </code></pre>
  <p>But this can't be the correct approach to solve this, I assume.</p>
  <ul>
  <li>The files are separated by an '&amp;' and not by the boundary from the header, so it is not possible to separate them.</li>
  <li>Somehow the framework should do the separation instead.</li>
  <li>The multipart body is a string and not a byte array.</li>
  <li>The webservice is blocked during the body read.</li>
  </ul>
  <p>I have read a lot about how to upload files in ASP.Net Core, about webapi-controllers, model view controllers, annotations, minimal API or not and so on,
  but I'm still not sure what the correct way to go is, I couldn't find a working minimal example.</p>
  <p>I would like to know:</p>
  <ul>
  <li>which Visual Studio 2022 starting project type should I use?</li>
  <li>what framework to use (minimal api, api with controllers, [ApiController] attribute ...)?</li>
  <li>the necessary attributes for POST - binary multipart/form-data?</li>
  </ul>