I have an ASP.net core web API with swagger (using swashbuckle).
One of the actions in the Web API is a file upload action:
[Produces("application/json")]
[Route("[controller]")]
public class FilesController : Controller
{
[HttpPost]
public void Post(IFormFile file)
{
...
}
}
When I look up that action in the swagger UI it let's me fill in all the fields of IFormFile
, which is not what I want to do to test my API.
So how can I add an upload button to the Swagger UI?
In addition to @Nick's answer, I have to make 2 changes for AspNet core 2.
1] Updated GetOperationId()
Now all operationIds contain API prefix as well as Method in the suffix. So I have statically added API & Post in ActionName.
2] Remove only file parameter
Instead of removing all parameters for that action, I want to remove only the file parameter.
First add a operation filter that consumes the multipart formdata.
Now you should add your actions to the
_actionWithUpload
array! Note that I added the extesnions only to have a refactoring friendly filter.Last but not least, make sure the operation filter is added to the options of swagger. So add
options.OperationFilter<FileUploadOperation>();
to your swagger options and done.Full example: