1

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 protected]&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":"[email protected]","MatrikelId":"132456asd"}

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

6
  • What is the Content-Type header value in your request? Commented Mar 4, 2013 at 10:16
  • I have not set one in fiddler the headders are the following "User-Agent: Fiddler Host: localhost:51268 Content-Length: 79" Commented Mar 4, 2013 at 10:24
  • try removing the [FromBody] infront of the parameters Commented Mar 4, 2013 at 10:27
  • @Aviatrix i have tryed that, did not work Commented Mar 4, 2013 at 10:27
  • In that case please set it to application/x-www-form-urlencoded it should resolve the issue. If you confirm it was your issue I will describe details in answer. Commented Mar 4, 2013 at 10:28

1 Answer 1

3

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:

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.