Given is the following webapi HttpPost method:
using Microsoft.AspNetCore.Mvc;
/// <summary>
/// Eviget controller used for uploading artefacts
/// Either from teamcity or in case of the misc files
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
public class UploadDemoController : ControllerBase
{
[HttpPost]
public IActionResult Upload([FromForm] UploadContent input)
{
return Ok("upload ok");
}
}
public class UploadContent
{
public string Id { get; set; }
public string Name { get; set; }
public Stream filecontent { get; set; }
}
The following code is used to upload a MultipartFormDataContent
using System.Net.Http.Headers;
HttpClient http = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
StringContent IdStringContent = new StringContent(Guid.NewGuid().ToString());
form.Add(IdStringContent, "Id");
StringContent NameStringContent = new StringContent($@"foobar");
form.Add(NameStringContent, "Name");
StreamContent TestStream = new StreamContent(GenerateStreamFromString("test content of my stream"));
TestStream.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "filecontent", FileName = "test.txt" };
TestStream.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
form.Add(TestStream, "filecontent");
//set http heder to multipart/form-data
http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("multipart/form-data"));
try
{
System.Console.WriteLine("start");
var response = http.PostAsync("http://localhost:5270/api/UploadDemo/Upload", form).Result;
response.EnsureSuccessStatusCode();
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
By default, the response is 400 (Bad Request).
With the following controller option, the request is sent to the rest server. This option just says the rest server should ignore null values.
builder.Services.AddControllers(options => options.SuppressImplicitRequiredAttributeForNonNullableReferenceTypes = true)
The stream is always null. (Note: The other values are properly set)

But the stream is actually part of the multipart form data(fiddler output)

What do i need to do that the Stream is properly mapped in this case?