Q: ASP.NET MVC API model not parsing I am having some problems parsing my model in ASP.NET MVC API
This is my API controller:
public class UserController : ApiController
{
    // Hent liste af personer
    public IEnumerable<UserModel> Get()
    {
        return new UserModel[] { new UserModel(), new UserModel() };
    }

    // Hente enkelt person
    public UserModel Get(int id)
    {
        return new UserModel();
    }

    // Opret person
    [ValidationActionFilter]
    public CreateUserRespose Post([FromBody]UserModel model)
    {
        CreateUserRespose rs = new CreateUserRespose();
        return rs;
    }

    // Rediger person
    public UserModel Put(int id, [FromBody]UserModel model)
    {
        return new UserModel();
    }

    // Slet person
    public UserModel Delete(int id)
    {
        return new UserModel();
    }
}

}
And the UserModel:
public class UserModel
{
    [Required]
    [StringLength(500)]
    public String FristName { get; set; }
    [Required]
    [StringLength(500)]
    public String LastName { get; set; }
    [Required]
    [StringLength(250)]
    public String Email { get; set; }
    [Required]
    public String MatrikelId { get; set; }
}

When I call though Fiddler to the Post command with the following body
FirstName=Fistname MiddleName&LastName=SomeName&Email=email@email.us&MatrikelId=1234

Will the action Post be called, but the model is null, and ModelState.IsValid is true, the same happens if I send no data with the body!
What am I doing wrong here?
Update:
I have tryed sending the data as json instead
Fiddler:
User-Agent: Fiddler
Host: localhost:51268
Content-Length: 102
Content-type: application/json

{"FristName":"Kasper asdasd","LastName":"asdasdasd","Email":"asdaasd@asdasd.us","MatrikelId":"132456asd"}

But should the model state not be invalid when the model is null?

A: The ASP.NET Web API is using content negotiation process in order to decide which MediaTypeFormatter to use for deserializing the body of the request. For the typical POST request it will check for Accept and Content-Type headers. If none is present it will use the first MediaTypeFormatter on the list (by default it is JsonMediaTypeFormatter).
In your case Web API was unable to determine the proper MediaTypeFormatter. Adding a Content-Type header with value of application/x-www-form-urlencoded to the request should resolve the issue.
If you want to get more detailed knowledge regarding Formatters, Model Binding and Content Negotiation in ASP.NET Web API I would suggest following reading:


*

*Designing Evolvable Web APIs with ASP.NET -> Chapter 8. Formatters and Model Binding (you should look very close at the entire book if you are interesed in learning ASP.NET Web API)

*Everything you want to know about ASP.NET Web API content negotiation
