在单个请求中上传文件作为较大表单提交的一部分(最少 API)

问题描述 投票:0回答:1

我正在尝试通过

IFormFileCollection
上传文档作为较大表单提交的一部分(在单个请求中)。每个文档都有附加元数据,例如文档类型、文档名称和文档描述。

enter image description here

我收到的错误消息:

执行请求时发生未处理的异常。 Microsoft.AspNetCore.Http.BadHttpRequestException:缺少属性“LegalEntity”所需的值。

这是我的有效负载:

{
  "legalEntity": {
    "name": "Acme Corporation",
    "shortName": "Acme Corp",
    "type": "Organization",
    "registrationNumber": "REG123456",
    "dateOfIncorporation": "2000-01-01T00:00:00",
    "taxIdentificationNumber": "TAX987654",
    "country": "United States",
    "industryType": "Technology",
    "creditLimit": 1000000.00,
    "contactInformation": {
      "primaryContactName": "John Doe",
      "primaryContactPhoneNumber": "+1 (555) 123-4567",
      "primaryContactEmail": "[email protected]",
      "secondaryContactName": "Jane Smith",
      "secondaryContactPhoneNumber": "+1 (555) 987-6543",
      "secondaryContactEmail": "[email protected]",
      "website": "https://www.acmecorp.com"
    },
    "addressInformation": {
      "street": "123 Tech Street",
      "city": "Silicon Valley",
      "state": "CA",
      "postalCode": "94000"
    },
    "bankInformation": {
      "accountName": "Acme Corporation Operating Account",
      "accountNumber": "1234567890",
      "swiftCode": "TECHUS123",
      "iban": "DE89370400440532013000",
      "bankAddress": "456 Bank Street, Silicon Valley, CA 94000, USA"
    },
    "kycInformation": {
      "status": "Pending",
      "riskRating": "Low",
      "amlStatus": "Compliant",
      "sanctionsCheckStatus": "Passed",
      "complianceNotes": "Initial compliance review completed",
      "parentCompany": null,
      "documents": [
        {
          "documentType": "Certificate of Incorporation",
          "documentName": "acme_incorporation.pdf",
          "documentDescription": "Certificate of Incorporation for Acme Corp"
        }
      ]
    },
    "uboInformation": {
      "name": "John Doe",
      "ownershipPercentage": 100.00,
      "nationality": "United States",
      "identificationNumber": "123-45-6789"
    }
  }
}

这是终点:

public class CreateLegalEntityEndpoint : IEndpoint
{
    public static void MapEndpoint(IEndpointRouteBuilder endpoints)
    {
        ArgumentNullException.ThrowIfNull(endpoints);

        endpoints.MapPost("/legalentities",
                async ([FromForm] CreateLegalEntityCommand command, ISender sender, CancellationToken cancellationToken) =>
                {
                    var result = await sender.Send(command, cancellationToken);
                    return result.Match(id => Results.Created($"/legalentities/{id}", id), CustomResults.Problem);
                })
            .Produces<Guid>(StatusCodes.Status201Created)
            .ProducesValidationProblem()
            .ProducesProblem(StatusCodes.Status500InternalServerError)
            .DisableAntiforgery() // See: https://learn.microsoft.com/en-us/dotnet/core/compatibility/aspnet-core/8.0/antiforgery-checks
            .WithTags(nameof(LegalEntity))
            .WithOpenApi(operation => new OpenApiOperation(operation)
            {
                Summary = "Create a new legal entity",
                Description = "Creates a new legal entity with all its related information"
            });
    }
}
public sealed record CreateLegalEntityCommand : IRequest<Result<Guid>>
{
    public required LegalEntityDto LegalEntity { get; init; }
    public required IFormFileCollection Documents { get; init; }
}
c# asp.net-core .net-8.0 minimal-apis
1个回答
0
投票

Andrew Lock 写了一篇关于在单个请求中混合 JSON 和文件有效负载的优秀文章:https://andrewlock.net/reading-json-and-binary-data-from-multipart-form-data-sections-in-aspnetcore /

© www.soinside.com 2019 - 2024. All rights reserved.