You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.NET6 Azure Functions v4 OpenAPI文件上传接口Swagger渲染失败

解决Azure Functions v4中OpenApi Swagger渲染multipart/form-data时的"Sequence contains no elements"错误

问题根源

你遇到的错误是因为Azure Functions的OpenApi扩展无法正确处理byte[]类型的模型属性,在生成Swagger Schema时会将其误判为列表类型,但找不到对应的元素定义,从而抛出序列为空的异常。

解决方案

将模型中的byte[]替换为ASP.NET Core原生的IFormFile类型,OpenApi扩展对该类型有内置支持,能正确生成符合multipart/form-data规范的Schema。

修改后的代码

1. 调整FormDataModel模型

using Microsoft.AspNetCore.Http;

public class FormDataModel
{
    public IFormFile FileUpload { get; set; }
}

2. 更新函数实现与配置

using Microsoft.AspNetCore.Http;
using Microsoft.Azure.Functions.Worker;
using Microsoft.Azure.Functions.Worker.Http;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Enums;
using System.Net;
using System.Threading.Tasks;

[OpenApiOperation(operationId: "newconversionjobstream", tags: new[] { "New Conversion Job Stream" }, Summary = "Convert the given file from a source mimetype to a target mimetype", Description = "Convert the given file from a source mimetype to a target mimetype.", Visibility = OpenApiVisibilityType.Important)]
[OpenApiSecurity("function_key", SecuritySchemeType.ApiKey, Name = "code", In = OpenApiSecurityLocationType.Query)]
[OpenApiParameter(name: "correlationid", In = ParameterLocation.Path, Required = true, Type = typeof(string), Summary = "Id to track request, can be used to correlate processing between different micro services", Description = "Id to track request", Visibility = OpenApiVisibilityType.Important)]
[OpenApiParameter(name: "jobid", In = ParameterLocation.Path, Required = true, Type = typeof(string), Summary = "jobId of from the schedule conversion job", Description = "jobId", Visibility = OpenApiVisibilityType.Important)]
[OpenApiRequestBody(contentType: "multipart/form-data", bodyType: typeof(FormDataModel), Required = true, Description = "Source File")]
[OpenApiResponseWithoutBody(statusCode: HttpStatusCode.Accepted, Summary = "The conversion job was accepted for processing. use the url on header to get the result", Description = "The conversion job was scheduled for processing. use the header url to get the job result")]
[Function("add-newconversion-job-stream")]
public async Task<HttpResponseData> ScheduleNewConversionJobStream(
     [HttpTrigger(AuthorizationLevel.Function, "post", Route = "{correlationid}/new-conversion-job-stream/{jobid}")] HttpRequestData req,
     string correlationid,
     string jobid,
     FunctionContext executionContext)
{
    // 通过模型绑定读取上传文件
    var formData = await req.ReadFormAsync<FormDataModel>();
    var uploadedFile = formData.FileUpload;

    // 这里添加你的文件处理逻辑

    var response = req.CreateResponse(HttpStatusCode.Accepted);
    // 可在此处添加结果查询的头部信息
    return response;
}

关键说明

  • IFormFile是ASP.NET Core专门用于处理文件上传的类型,OpenApi扩展能自动识别并生成正确的multipart/form-data请求Schema。
  • 替换后Swagger UI和JSON将正常渲染,同时也能通过模型绑定更规范地获取上传文件,无需直接操作req.Form.Files。

内容的提问来源于stack exchange,提问作者AmilaKR

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 10:51:09