ASP.NET Web API does not return XML

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)
        {
            // Add user to DB
            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)
        {
            // Return the validation errors in the response body.
            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.

+5
source share
2 answers

:

var errors = new Dictionary<string, IEnumerable<string>>();
actionContext.Request.CreateResponse(HttpStatusCode.BadRequest, errors);

, errors, Dictionary<string, IEnumerable<string>>();.

Web.API MediaTypeFormatter . XML- (DataContractSerializer) Dictionary<string, IEnumerable<string>>();, JSON.

CreateErrorResponse ModelState ( HttpError, XML seriazable)

public class ModelValidationFilterAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(HttpActionContext actionContext)
    {
        if (actionContext.ModelState.IsValid == false)
        {
            actionContext.Response =
                actionContext.Request.CreateErrorResponse(
                    HttpStatusCode.BadRequest, 
                    actionContext.ModelState);
        }
    }
}
+6

, Web API JSON .

JSON, ?

http://www.asp.net/web-api/overview/formats-and-model-binding/json-and-xml-serialization

i.e.

void ConfigureApi(HttpConfiguration config)
{

    // Remove the JSON formatter
    config.Formatters.Remove(config.Formatters.JsonFormatter);
}
+1
source

All Articles