Search code examples
.netfile-uploadswaggerswagger-ui

Swagger UI Not Displaying Upload Button for File Input in ASP.NET Core Web API


I'm working on an ASP.NET Core Web API project, specifically using the ASP.NET Core Web API template provided by Microsoft. I've encountered an issue where Swagger UI is not displaying the upload button for a controller action that expects a file as input.

Setup and Configuration:

  1. Project Template: The project was created using the ASP.NET Core Web API template. This includes a default WeatherForecast API with Swagger support.

  2. Swagger Configuration in Program.cs:

    builder.Services.AddSwaggerGen();
    if (app.Environment.IsDevelopment())
    {
        app.UseSwagger();
        app.UseSwaggerUI();
    }
    
  3. .csproj File:

    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <Nullable>enable</Nullable>
        <ImplicitUsings>enable</ImplicitUsings>
        <InvariantGlobalization>true</InvariantGlobalization>
      </PropertyGroup>
    
      <ItemGroup>
        <PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
      </ItemGroup>
    </Project>
    
  4. Controller Action in WeatherForecastController.cs:

    [HttpPost]
    public IActionResult UploadFile([FromForm] IFormFile file)
    {
        return Ok();
    }
    

Issue:

After starting the application via Visual Studio as an HTTP instance, the Swagger UI does not show the expected upload button for the file input. The current Swagger UI looks like this:

Current Swagger UI

Expected Result:

I am expecting to see an upload button similar to this:

Expected Swagger UI

Could anyone suggest why the upload button isn't appearing and how I might resolve this issue?


Solution

  • Found an issue...

    For the Swashbuckle.AspNetCore version which was used 6.5 and .net 8 - Handle Forms and File Uploads. Just need to omit [FromForm]. And looks like based on official Microsoft documentation this attribute shouldn't be used for .net core 2.2+.

    [HttpPost]
    [Route("")]
    public IActionResult Post(IFormFile file)
    {
    }