没有内置机制来处理这种类型的发布数据(multipart/mixed
实际上具有无限的可能性,并且很难在一般意义上绑定到它),但是,您可以使用以下方法轻松地自己解析数据MultipartReader https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.webutilities.multipartreader object.
我假设所有传入的数据都有以下处理:attachment
并且只有 JSON 和 XML 内容类型有效。但这应该是足够开放的,以便您可以根据需要进行修改。
看看这个静态助手:
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.AspNetCore.WebUtilities;
using Microsoft.Net.Http.Headers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Net.Mime;
using System.Text;
namespace YourNamespace.Utilities
{
public static class MutipartMixedHelper
{
public static async IAsyncEnumerable<ParsedSection> ParseMultipartMixedRequestAsync(HttpRequest request)
{
// Extract, sanitize and validate boundry
var boundary = HeaderUtilities.RemoveQuotes(
MediaTypeHeaderValue.Parse(request.ContentType).Boundary).Value;
if (string.IsNullOrWhiteSpace(boundary) ||
(boundary.Length > new FormOptions().MultipartBoundaryLengthLimit))
{
throw new InvalidDataException("boundry is shot");
}
// Create a new reader based on that boundry
var reader = new MultipartReader(boundary, request.Body);
// Start reading sections from the MultipartReader until there are no more
MultipartSection section;
while ((section = await reader.ReadNextSectionAsync()) != null)
{
// parse the content type
var contentType = new ContentType(section.ContentType);
// create a new ParsedSecion and start filling in the details
var parsedSection = new ParsedSection
{
IsJson = contentType.MediaType.Equals("application/json",
StringComparison.OrdinalIgnoreCase),
IsXml = contentType.MediaType.Equals("text/xml",
StringComparison.OrdinalIgnoreCase),
Encoding = Encoding.GetEncoding(contentType.CharSet)
};
// Must be XML or JSON
if (!parsedSection.IsXml && !parsedSection.IsJson)
{
throw new InvalidDataException("only handling json/xml");
}
// Parse the content disosition
if (ContentDispositionHeaderValue.TryParse(
section.ContentDisposition, out var contentDisposition) &&
contentDisposition.DispositionType.Equals("attachment"))
{
// save the name
parsedSection.Name = contentDisposition.Name.Value;
// Create a new StreamReader using the proper encoding and
// leave the underlying stream open
using (var streamReader = new StreamReader(
section.Body, parsedSection.Encoding, leaveOpen: true))
{
parsedSection.Data = await streamReader.ReadToEndAsync();
yield return parsedSection;
}
}
}
}
}
public sealed class ParsedSection
{
public bool IsJson { get; set; }
public bool IsXml { get; set; }
public string Name { get; set; }
public string Data { get; set; }
public Encoding Encoding { get; set; }
}
}
您可以从端点调用此方法,如下所示:
[HttpPost, Route("TestMultipartMixedPost")]
public async Task<IActionResult> TestMe()
{
await foreach (var parsedSection in MutipartMixedHelper
.ParseMultipartMixedRequestAsync(Request))
{
Debug.WriteLine($"Name: {parsedSection.Name}");
Debug.WriteLine($"Encoding: {parsedSection.Encoding.EncodingName}");
Debug.WriteLine($"IsJson: {parsedSection.IsJson}");
Debug.WriteLine($"IsXml: {parsedSection.IsXml}");
Debug.WriteLine($"Data: {parsedSection.Data}");
Debug.WriteLine("-----------------------");
}
return Ok();
}
您的端点将输出:
Name: part1
Encoding: Unicode (UTF-8)
IsJson: False
IsXml: True
Data: <foo>bar</foo>
-----------------------
Name: part2
Encoding: Unicode (UTF-8)
IsJson: True
IsXml: False
Data: {"foo": "bar"}
-----------------------
此时,您必须根据返回对象的属性进行反序列化。