Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC4 RC WebApi parameter binding

I upgraded from MVC4 beta to RC and the latest autofac. The following action was binding properly, but now both parameters are null. I see they changed things about the Formatters and such but I am not sure what caused my problem

[HttpPost]    
RedirectModel MyAction(string value1, string value1)

REQUEST

Method: POST
Accept: application/json
URL: api/controller/myaction
BODY: {"value1":"1000", "value2":"foo"}
like image 444
Thad Avatar asked Jun 08 '12 20:06

Thad


People also ask

What is parameter binding in Web API?

Binding is a process to set values for the parameters when Web API calls a controller action method. Web API methods with the different types of the parameters and how to customize the binding process.

How do I pass body parameters in Web API?

Use [FromUri] attribute to force Web API to get the value of complex type from the query string and [FromBody] attribute to get the value of primitive type from the request body, opposite to the default rules.

What is model binding in Web API?

Model Binding is the most powerful mechanism in Web API 2. It enables the response to receive data as per requester choice. i.e. it may be from the URL either form of Query String or Route data OR even from Request Body. It's just the requester has to decorate the action method with [FromUri] and [FromBody] as desired.


1 Answers

When you want to avoid using a DTO object, try this:

[HttpPost]    
RedirectModel MyAction(dynamic value1, dynamic value2) {
    string sValue1 = value1;
    string sValue2 = value2;
like image 73
mhu Avatar answered Oct 08 '22 03:10

mhu