I created a VS 2012 MVC API project from scratch, loaded the Nuget "WebApiContrib.Formatting.Jsonp", added a route, the formatter, and tried to send parameters as serialized JSON as a JSONP request. How do I identify or access these parameters in the controller?
WebApiConfig:
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}/{format}",
defaults: new { id = RouteParameter.Optional,
format = RouteParameter.Optional }
);
config.Formatters.Insert(0, new JsonpMediaTypeFormatter());
Api Method:
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
JQuery:
$.ajax({
type: 'GET',
url: '/api/Values',
data: JSON.stringify({ "message": "\"Hello World\"" }),
contentType: "application/json; charset=utf-8",
dataType: 'jsonp',
success: function (json) {
console.dir(json.sites);
},
error: function (e) {
console.log(e.message);
}
});
I've tried modifying the Api method to include:
Get( [FromUri] string value)
Get( [FromBody] string value)
Get( CustomClass stuff)
Get( [FromUri] CustomClass stuff)
Get( [FromBody] CustomClass stuff)
Where CustomClass is defined as:
public class CustomClass
{
public string message { get; set; }
}
So far, none of these produce anything but null for the parameters. How should I wire up these parameters from the object posted in the querystring?
EDIT:
I can trick it by modifying the JQuery ajax to be:
data: {"value":JSON.stringify({ "message": "\"Hello World\"" })}
for which I can de-stringify the [FromUri] string value
and get my strongly typed object.
Still, I'm expecting the data binder to de-serialize parameters in to strongly typed objects. What technique makes this happen?