I have to implement a method that i declared like this:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = @"whatever")]
void MyMethod(InputContainer input);
where InputContainer
is declared as a DataContract
:
[DataContract(Namespace = "whatever")]
public class InputContainer : IExtensibleDataObject
{
//[DataMember]s inside
}
Now in my code I have an action to which the call will be mapped:
[HttpPost]
ActionResult MyMethod()
{
// How do I get the InputContainer object here?
}
I've only seen samples using ReadAsAsync()
which is a dependency on one more assembly which I'd like t avoid. Anyway I suspect ReadAsAsync()
is some helper method that I could replicate.
How do I get the DataContract
-attributed object POST
ed to my MVC3 action?
This won't work out of the box. If I add a parameter of type InputContainer
to my action - it will be default-initialized, the values in the POST body will be ignored.
Extra wiring from here is needed. First an implementation of IModelBinder
is needed that will access the HTTP request InputStream
and deserialize the object with DataContractSerializer
. Then a subclass of CustomModelBinderAttribute
is needed that would return the abovementioned IModelBinder
implementations form its GetBinder()
implementation. Let's pretend it is called MagicAttribute
. Once all this is done all is needed is adding the attribute to the parameter:
[HttpPost]
ActionResult MyMethod([Magic] InputContainer input)
{
}