首页 > 解决方案 > 是否可以将 System.IO.Stream 作为 Swagger 的输入参数?

问题描述

我们正在为一个项目使用 swagger (v 2.0),其中一个 ASP.NET API 控制器具有接收 [FromBody] System.IO.Stream 的函数。但是当我启动 swagger ui 时,UI 会提示一些在使用和调试时,流总是为空的。在线研究时,我看到许多其他人使用 IFromFile,但这似乎是 .NET Core 独有的。

我正在尝试做的事情是否可行,或者我应该尝试其他事情?提前致谢。

当前使用的代码:

    /// <summary>
    /// Store the photo of the object.
    /// </summary>
    /// <remarks>Registers the given object photo that was captured through scanning. </remarks>
    /// <param name="barcode">Barcode of the scanned object </param>
    /// <param name="lang">Language in which the request should be processed, if possible (e.g., for object information retrieval and status message construction).  This is the MD Connect GUI’s language. Expressed as a LanguageCode (see Definitions). </param>
    /// <param name="photo"></param>
    /// <response code="201">The captured object photo was successfully registered.</response>
    /// <response code="400">Bad Request</response>
    /// <response code="500">Internal Server Error</response>
    [HttpPost]
    [Route("api/logistore/multiscan/scanhandler/v1/objects/{barcode}/scan/photos")]
    [SwaggerOperation("RegisterCapturedObjectPhoto")]
    [SwaggerResponse(200, type: typeof(HttpStatusCode))]
    [SwaggerResponse(201, type: typeof(HttpStatusCode))]
    [SwaggerResponse(400, type: typeof(ErrorResult))]
    [SwaggerResponse(500, type: typeof(ErrorResult))]
    public IHttpActionResult RegisterCapturedObjectPhoto([FromUri]string barcode, [FromUri]string lang, [FromBody]System.IO.Stream photo)
    {
        if (string.IsNullOrWhiteSpace(barcode))
        {
            throw new RestException(HttpStatusCode.BadRequest, "UNKNOWN_BARCODE", "Unknown barcode / Onbekende barcode: Barcode cannot be empty.");
        }

        if (photo == null)
        {
            throw new RestException(HttpStatusCode.BadRequest, "INVALID_PHOTO", "Invalid photo / Ongeldige foto: Invalid input for photo received.");
        }

        return Content(HttpStatusCode.Created, "The captured object photo was successfully registered.");
    }

Swagger UI 建议:

大摇大摆的 UI 建议

标签: asp.netstreamswaggerswagger-ui

解决方案


推荐阅读