1

Does ASP.NET Web API 2 (not core) contain also something like [FromForm] attribute for binding the action? Or can I add the dll from ASP.NET Core to a normal ASP.NET Web API 2? {

"Message": "The request entity's media type 'multipart/form-data' is not supported for this resource.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'FooBindModel' from content with media type

'multipart/form-data'.", "ExceptionType": "System.Net.Http.UnsupportedMediaTypeException", "StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)" }

3 Answers 3

2

No, only FromUri and FromBody is available for ASP.NET Web API 2. You can look at the official documentation.

Also, why would you try to implement FromForm to ASP.NET Web API 2. You can simply configure your form action as GET and POST and pass the data to Web Api. In my opinion, it would be over engineering.

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

3 Comments

@Stormcloack well I am currently using Postman to send my data (integers and a file)... however i am not able to bind my integers with my bind model...
Why ? Just post your file and integers. What would FromForm provide you something in this case ?
@Stormcloack .... I am saying that if I use [FromBody] I am not able to bind the data...
1

I don't think you actually understand these attributes or what they do. [FromBody] means that you are providing a serialization (JSON, XML, etc.) or basically any other content that doesn't have an application/x-www-form-urlencoded mime type (image or other file type). If you're sending a normal form post (application/x-www-form-urlencoded request body) to a param marked as [FromBody], of course it won't bind, because you're explicitly telling it that it should be expecting something other than what you're sending.

Something like [FromForm] is actually pretty useless anyways, as the default is to expect application/x-www-form-urlencoded, which is all this attribute would do. Long and short, just don't decorate your action param with anything.

1 Comment

well my bind model still doesn't bind the primitive types, no idea why... although I am able to get my file(image) from HttpContext.Current.Request.Files[0]; PS: I am using ASP.NET NOT core, in my WebApiConfig I have this line of code config.Formatters.XmlFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data")); because I get an awful error if I don't have it(error in question)
1

You have to use FromUri for your model and also use HttpContext.Current.Request.Files for getting files from the header

this is your model that set in the Params section enter image description here

this is your file that set in the Body section ==> from-data enter image description here and this is final result enter image description here

Comments

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.