我有一个Controller,它使用表单数据接受IFormFile
和对象(称为Document
的类)。
这是控制器:
[HttpPost]
public async Task<IActionResult> Post(IFormFile file, [FromForm] Document document, CancellationToken token = default)
{
...
}
Document
类的外观如下:
public class Document
{
public Guid DocumentId { get; set; }
public string Name { get; set; }
public DocumentType DocumentType { get; set; } = DocumentType.Unsorted;
public Dictionary<string, string> Metadata { get; set; } = new Dictionary<string, string>();
}
这是POST
的数据发送到所述控制器的代码:
using (var multipartContent = new MultipartFormDataContent())
{
multipartContent.Add(new StringContent(document.DocumentId.ToString()), FormDataKeys.DocumentId);
multipartContent.Add(new StringContent(document.DocumentType.ToString()), FormDataKeys.DocumentType);
multipartContent.Add(new StreamContent(file), FormDataKeys.File, document.Name);
using (var apiResult = await _httpClient.PostAsync("api/documents", multipartContent, token))
{
var content = await apiResult.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<StoreDocumentResult>(content);
}
}
此方法有效,当我发送POST请求时,控制器中的IFormFile
和[FromForm] Document
参数的属性都被填写。只是,我对如何填充Metadata
的{{1}}属性一无所知?如何在Document
中传递Dictionary<string, string>
?
答案 0 :(得分:2)
最简单的方法是使用JSON将字典序列化为字符串,然后反序列化。
var settings = new JsonSerializerSettings();
settings.Formatting = Formatting.Indented;
settings.ContractResolver = new DictionaryAsArrayResolver();
// serialize
string json = JsonConvert.SerializeObject(Document.Metadata, settings);
和
multipartContent.Add(new StringContent(json ), FormDataKeys.Metadata );
要反序列化它,您可以使用如下代码:
var d = JsonConvert.DeserializeObject<Dictionary<String,String>>(json, settings);
另一个选择是子类化HttpContent并重写SerializeToStreamAsync方法。在这种情况下,您可以根据需要将其写入提供的缓冲区。
class DictionaryContent: HttpContent
{
public Object Value { get; }
public DictionaryContent( Object value)
{
Value = value;
Headers.ContentType = .. You must provide the desired content type.
}
protected override Task SerializeToStreamAsync( Stream stream, TransportContext context )
{
using ( var buffer = new BufferStreamWriter( stream, 4096 ) )
{
var writer = new JsonWriter( buffer, JsonSettings.Default );
writer.WriteValue( Value ); // HERE You can do anything that you want.
}
return Task.CompletedTask;
}
}