Search code examples
angularjsweb-servicesasp.net-web-apiasp.net-web-api2odata

Unable to Post JSON data to OData WEBAPI Service Action


I am trying consume my OData Action and trying to post a JSON Data as:

{
  "contractId":[1,3,5,7,9],
  "projectId":9
}

My Action is configured as :

var deleteContracts = builder.EntityType<Contract>().Collection.Action("DeleteItems").Returns<HttpResponseMessage>();
deleteContracts.CollectionParameter<long>("C_Id");
deleteContracts.Parameter<long>("P_Id");

Action is as below:

[HttpPost]
public IHttpActionResult DeleteItems (ODataActionParameters param)
 {
    //DO something
    return null;
 }

While I am trying to call this, I am getting the following issue:

"error": {
"code": "",
"message": "The request entity's media type 'application/x-www-form-urlencoded' is not supported for this resource.",
"innererror": {
"message": "No MediaTypeFormatter is available to read an object of type 'ODataActionParameters' from content with media type 'application/x-www-form-urlencoded'.",
"type": "System.Net.Http.UnsupportedMediaTypeException", "stacktrace": " at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken) at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable`1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"
}
}

Response header:

Content-Type: application/json; odata.metadata=minimal

Server: Microsoft-IIS/10.0 Odata-Version: 4.0 X-Sourcefiles: =?UTF-8?B?QzpcVXNlcnNcbG1pc2hyYVxEb2N1bWVudHNcTGF4bWlrYW50XF9wcm9qZWN0c1xXZWJcRGV2ZWxvcG1lbnRcQ29udHJhY3RzXEluRWlnaHQuQ29udHJhY3RzLldlYlNlcnZpY2VzXG9kYXRhLWNvbnRyYWN0c1xDb250cmFjdHNcRGVmYXVsdC5EZWxldGVTZWxlY3RlZENvbnRyYWN0?= X-Powered-By: ASP.NET Date: Fri, 25 Nov 2016 04:45:35 GMT Content-Length: 786

Request Header:

POST /ContractsWebServices/ContractsWebService/odata-contracts/Contracts/Default.DeleteItems HTTP/1.1

HOST: localhost:65314 content-type: application/json content-length: 50

{ "contractId":[1,3,5,7,9], "projectId":9 }

Any help is appreciated. Thanks.


Solution

  • Nuget doesn't resolve oData dlls version properly. in project Odata version for routing was v2/3 but implementation was done for newly added controller as per v4.