I have this class:
public class MyClass { public MyClass() { Secret = "Don't tell me"; } public string Name { get; set; } public int Age { get; set; } public string Description { get; set; } private string Secret { get; set; } }
And this WEB API method:
// POST api/fixture public HttpResponseMessage Post(MyClass value) { return new HttpResponseMessage(HttpStatusCode.Created); }
I installed the Web API to return JSON instead of XML, and I did not make any other changes to the default configuration. I am trying to test this method with the RESTClient extension of Firefox. Here is my request:
POST localhost:XXXX/api/fixture Content-Type: application/json Accept: application/json Content-Length: 60 { value : { "Name":"Cosby","Age":"13","Description":"OK" } }
However, I get this error:
{"Message": "The media type of the message" text / plain "is not supported for this resource.", "ExceptionMessage": "No MediaTypeFormatter is available for reading an object of type MyClass from the contents with the environment, enter" text / plain ".," ExceptionType " : "System.Net.Http.UnsupportedMediaTypeException", "StackTrace": "in System.Net.Http.HttpContentExtensions.ReadAsAsync [T] (HttpContent content, type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable 1 formatters, IFormatterLogger formatterLogger, CancellationToken cancelationToken) \ r \ n in System.Web.Http.ModelBinding.FormatterParameterBindingCessRead.eadRestReadRead.readTestReadRead.eadRead.eadmentead IEnumerable`1 formatters, IFormatterLogger formatterLogg er, CancellationToken cancelationToken) "}
Edit:
I do not understand, because it seems that the method is not even called. If I debug, I see that the constructor is being called, and then no other method is being called. There are no exceptions.
I have been dealing with this problem for many years. I found that this problem usually occurs when the Content-Type is set incorrectly, but this does not seem to be my case, since the request is not even processed.
Any idea?
thanks
json asp.net-web-api asp.net-web-api2
reddy
source share