This project is read-only.

MVC Like Model Binding?

Topics: Web Api
Sep 26, 2011 at 6:38 PM

Does WCF Web API add support for MVC like model binding. If so, can someone please direct towards samples/examples? Thanks!

Sep 26, 2011 at 11:09 PM

From my perspective the answer is yes and beyond with the added IOC container support. Have you looked at the examples that are in the Source Download? Did you see the Contacts Advanced sample? If not, just go tot he downloads tab and download the source code which contains the examples.


\ ^ / i l l

Sep 27, 2011 at 8:14 AM

Web api supports two way model binding in two different forms.

1. You can use formatters to allow you to translate between an entity body representation to an object parameter on an operation. Out of the box we support json, xml and data (needs to be enabled). With MapServiceRoute you also get Form url encoding useful for browser forms. You can add your own custom formatters very easily. If you look at our ContactManager_Advanced sample you will see how to plug in formatters. For example you will see a custom v-card formatter which translates contact information into the well-known v-card format.

2. You can use operation handlers to translate from any part of the request / response such as a uri to an object. For example you could write an operation handler that takes a url like http://users/glenn, grabs "glenn" and turns it into the Glenn user object. We need more samples on this, but there are some great blog posts on how to use our operation handlers.

Both formatters and operation handlers execute before and after the method invocation thus like model binding they remove all the logic from within the operation itself.



Sep 27, 2011 at 10:14 AM

This might be of interest: