Back in the day of ASP.NET MVC 3, I used JsonValueProviderFactory
as per Phil Haack's Blog to bind JSON data as a Query String parameter to a strongly typed parameter.
Today, I find this isn't working. In MVC Web API, they seem to only bind POSTED JSON data to strongly typed objects (JSON in the body of the request), but not JSON data in the query string.
Is this the case with MVC 4 or what am I doing wrong?
My route:
routes.MapRoute(
name: "CreateUser",
url: "{controller}/CreateUser/{userAccount}",
defaults: new { action = "CreateUser"} );
My method:
public JsonResult CreateUser( MyObjectType userAccount)
{ /* user is null */ }
My Class:
public class MyObjectType
{
public string CardNumber {get;set;}
}
More tests:
// This completely fails, even if you URL Encode it:
localhost/CreateUser/{"CardNumber":"11111111"}
// Creates the object but all properties are null
localhost/CreateUser?{"CardNumber":"11111111"}
localhost/CreateUser?userAccount={"CardNumber":"11111111"}
I've also tried serializing all properties of the object even if we don't need the values, ensured the names are identical in spelling and in case, etc.
I can grab the parameter as string then deserialize it with JSON.NET without an issue, but I also want to use DataAnnotations
to check the ModelState.IsValid
.