Home > Software engineering >  Difference between annotation and request object
Difference between annotation and request object

Time:10-13

I want to ask if there is a difference between the annotation [FromForm] and Request.Form. Is there a difference in the way they access the Request object? For reference this is the code

public IActionResult Test ([FromForm] IFormFile File1)
{
      var File2 = Request.Form.File[0];
}

If there is a difference then how do you send the file in case of [FromForm] from a angular app.

CodePudding user response:

  1. The [FromForm] annotation merely tells the compiler that the value for argument "File1" should come from the request object's "form".

  2. Here is the documentation:

https://learn.microsoft.com/en-us/aspnet/core/mvc/models/model-binding

  • FromQuery - Gets values from the query string.
  • FromRoute - Gets values from route data.
  • FromForm - Gets values from posted form fields.
  • FromBody - Gets values from the request body.
  • FromHeader - Gets values from HTTP headers.
  1. Your action handler, "Test()", has "File1" as a parameter.

    But "Test()" should also be able to read "File1" from the Request.Form object.

    Different "abstractions" - same thing.

  • Related