I have a controller and a method that adds the user to the database.
I call this from Fiddler the request header as follows:
Content-Type: application / xml
Accept: application / xml
Host: localhost: 62236
Content-Length: 39
And the request body is
<User>
<Firstname>John</Firstname>
<Lastname>Doe</Lastname>
</User>
This works, as expected, the method is called, the user object is processed in the PostUser method.
public class UserController : ApiController
{
public HttpResponseMessage PostUser(User user)
{
var response = new HttpResponseMessage(HttpStatusCode.Created);
var relativePath = "/api/user/" + user.UserID;
response.Headers.Location = new Uri(Request.RequestUri, relativePath);
return response;
}
}
I am doing model validation in my class
public class ModelValidationFilterAttribute : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
if (actionContext.ModelState.IsValid == false)
{
var errors = new Dictionary<string, IEnumerable<string>>();
foreach (KeyValuePair<string, ModelState> keyValue in actionContext.ModelState)
{
errors[keyValue.Key] = keyValue.Value.Errors.Select(e => e.ErrorMessage);
}
actionContext.Response =
actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);
}
}
}
BUT if I publish the following
<User>
<Firstname></Firstname> **//MISSING FIRST NAME**
<Lastname>Doe</Lastname>
</User>
The model is invalid and a JSON response is returned, although I said Accept: application / xml.
If I perform model validation in UserController, I get the correct XML response, but when I execute it in ModelValidationFilterAttribute, I get JSON.