I'm getting the above error and I'm not sure how to resolve it.
I have the following in my model...
public class PUpdate
{
public int customer_urn { get; set; }
public int amount { get; set; }
public string update_type { get; set; }
}
public class CreateUpdate : PUpdate
{
}
and when I try and run a POST test using Postman I get with above error.
The controller is
public string Post([FromBody]CreateUpdate value)
{
_conPU = new SqlConnection(ConfigurationManager.ConnectionStrings["KRConnectionString"].ConnectionString);
var query = "insert into KR.L_AMT (ACUID, AMT, aDATE ,COMMENTS) values (@ACUID,
@AMT, getdate() ,'Web')";
SqlCommand insertcommand = new SqlCommand(query, _conPU);
_conPU.Open();
int result = insertcommand.ExecuteNonQuery();
if (result > 0)
{
return "inserted";
}
else
{
return "failed";
}
}
I've also got the following in my webapiconfig.cs
config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new System.Net.Http.Headers.MediaTypeHeaderValue("application/json"));
var json = config.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
config.Formatters.Remove(config.Formatters.XmlFormatter);
The full error I get back is...
{
"Message": "An error has occurred.",
"ExceptionMessage": "No MediaTypeFormatter is available to read an object of type 'CreateUpdate' from content with media type 'text/plain'.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n at System.Net.Http.HttpContentExtensions.ReadAsAsync(HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Tracing.Tracers.FormatterParameterBindingTracer.<>c__DisplayClass3.<ExecuteBindingAsync>b__1()\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEndAsync(ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action`1 beginTrace, Func`1 execute, Action`1 endTrace, Action`1 errorTrace)\r\n at System.Web.Http.Tracing.Tracers.FormatterParameterBindingTracer.ExecuteBindingAsync(ModelMetadataProvider metadataProvider, HttpActionContext actionContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Controllers.HttpActionBinding.<>c__DisplayClass1.<ExecuteBindingAsync>b__0(HttpParameterBinding parameterBinder)\r\n at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()\r\n at System.Threading.Tasks.TaskHelpers.IterateImpl(IEnumerator`1 enumerator, CancellationToken cancellationToken)"
}
Any pointers to what I'm doing wrong would be grateful. Thanks.
What I have tried:
I've tried changing to only having one variable in the json but still get the same error. My variables need to be of both type int and string.