79154133

Date: 2024-11-04 03:35:29
Score: 2
Natty:
Report link

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)
{
    // ....
}
Reasons:
  • RegEx Blacklisted phrase (1.5): how to resolve this?
  • RegEx Blacklisted phrase (3): Can anyone help
  • Long answer (-0.5):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • High reputation (-2):
Posted by: Zhi Lv