I have an upload api
that receives a list of IFiles
to upload to a server. I now require an additional string to describe the upload for information details. My current controller looks like:
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files)
{
// upload to server and other stuff
}
To access the string, I have changed the controller:
[HttpPost("UploadFiles")]
public async Task<IActionResult> Post(List<IFormFile> files, string uploadDescription)
{
//some code
}
I now have access to the uploadDescription
. When testing this I am sending the files as MultipartFormDataContent
, and the description as a query parameter.
Although this works, I am not sure sending query parameters in this way with a post
action is best practice or not. Is there a better way to do this?
There is no best practice for doing such a Post
method, but from my knowledge this is better and readable to pass a unit DTO to your method either Post
or Get
, so I think it would be better to change it like :
public class FileSpec
{
public List<IFormFile> Files {get; set;}
public string UploadDescription {get; set;}
}
and change your method signature like :
public async Task<IActionResult> Post([FromBody]FileSpec fileSpec)
anyway this is depend on yourself you pass parameters separately or pass them as a DTO.
you can also read more about parameter binding here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.