I know in .NET 8 the [FromForm] attribute is not needed so I removed that from the Angular UI and passing it as formdata. Not sure why still the data is coming as empty but file is passed. Can anyone help how to resolve this?
When the [ApiController]
attribute applies an inference rule for action parameters of type IFormFile
and IFormFileCollection
. The multipart/form-data
request content type is inferred for these types, so there is no need to add the [FromForm]
attribute. But for the string type parameters it is no easier to inferred. So, we have to add the FromForm
attribute before it. Since the parameter might be empty, you can modify your code as below:
public async Task<ActionResult> upload(IFormFile file, [FromForm]string? data)
{
// ....
}
or
public async Task<ActionResult> upload([FromForm]IFormFile file, [FromForm]string? data)
{
// ....
}