2

I have created a brand new project in VS 2017 - by using the Web API template. But I am not able to use [FromForm] attribute in the controller action method.

Here some lines from package.config:

<package id="Microsoft.AspNet.Mvc" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Razor" version="3.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net461" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net461" />

Is the [FromForm] available in Web API version 5.2.3?

If not then how to read FormData values?

Code:

[HttpPost]
[Route("v1/users/register")]
public void RegisterUser(FormDataCollection formData)
{
    var fName = formData["firsName"]; // formData is NULL
}

Postman request:

enter image description here

13
  • 1
    FromForm attribute intends to asp core. You cannot use it in .net web api project. Commented Oct 5, 2019 at 15:53
  • Then how I can read FormData in Web API 5.2.3? Commented Oct 5, 2019 at 16:00
  • You look at books.google.co.il/… Commented Oct 5, 2019 at 16:02
  • @Oleg you mean FormValueCollection will do the work? Commented Oct 5, 2019 at 16:18
  • Yes, you can also try do it with model as parameter without any model binding attributes. Commented Oct 5, 2019 at 16:19

1 Answer 1

7

You can get form data from

HttpContext.Current.Request.Form: key and value.

Files: HttpContext.Request.Form.Files.

Post action must be without input or can be with HttpRequest parameter, like:

RegisterUser(HttpRequest request) and request.Form....

Sign up to request clarification or add additional context in comments.

1 Comment

Can we bind model with Request.Form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.