无法将JSON数据发布到OData WEBAPI服务操作

时间:2016-11-24 14:09:19

标签: angularjs web-services asp.net-web-api asp.net-web-api2 odata

我正在尝试使用我的OData Action并尝试将JSON数据发布为:

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

我的操作配置为:

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

行动如下:

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

虽然我试图打电话给我,但我收到了以下问题:

  

&#34;错误&#34;:{
             &#34;代码&#34;:&#34;&#34;,
           &#34; message&#34;:&#34;请求实体的媒体类型&#39; application / x-www-form-urlencoded&#39;此资源不支持。&#34;,
             &#34; innererror&#34;:{
             &#34; message&#34;:&#34;没有MediaTypeFormatter可用于读取类型&#39; ODataActionParameters&#39;来自内容与媒体类型&#39; application / x-www-form-urlencoded&#39;。&#34;,
             &#34;输入&#34;:&#34; System.Net.Http.UnsupportedMediaTypeException&#34;,              &#34; stacktrace&#34;:&#34;在System.Net.Http.Http.HttpContentExtensions.ReadAsAsync [T](HttpContent内容,类型类型,IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)处于System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage请求,类型类型, IEnumerable`1格式化程序,IFormatterLogger formatterLogger,CancellationToken cancellationToken)&#34;
         }
      }

响应标题:

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

服务器:Microsoft-IIS / 10.0 Odata版本:4.0 X-Sourcefiles:????= UTF-8乙QzpcVXNlcnNcbG1pc2hyYVxEb2N1bWVudHNcTGF4bWlrYW50XF9wcm9qZWN0c1xXZWJcRGV2ZWxvcG1lbnRcQ29udHJhY3RzXEluRWlnaHQuQ29udHJhY3RzLldlYlNlcnZpY2VzXG9kYXRhLWNvbnRyYWN0c1xDb250cmFjdHNcRGVmYXVsdC5EZWxldGVTZWxlY3RlZENvbnRyYWN0 = X-Powered-By:ASP.NET 日期:2016年11月25日星期五04:45:35 GMT 内容长度:786

请求标题:

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

主持人:localhost:65314 content-type:application / json 内容长度:50

{   &#34; contractId&#34;:[1,3,5,7,9],   &#34;专案编号&#34;:9 }

感谢任何帮助。 谢谢。

2 个答案:

答案 0 :(得分:0)

您的HTTP请求标头中有什么内容?

您应在标题中添加以下内容以指明请求格式。

Content-Type: application/json

编辑:

有两点需要注意:

  1. 不要将HttpResponseMessage用作响应类型,否则元数据看起来会很奇怪。只需使用您想要返回的实际实体或基本类型,如果没有返回则可以将其留空
  2. 参数名称不匹配: contractId-&gt; C_Id,projectId-&gt; P_Id
  3. 但根据消息,问题仍然是发送错误的标题。

    请尝试以下代码:

    namespace wea1
    {
        using System.Web.Http;
        using System.Web.OData;
        using System.Web.OData.Builder;
        using System.Web.OData.Extensions;
    
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                var builder = new ODataConventionModelBuilder();
                var deleteContracts = builder.EntityType<Contract>().Collection.Action("DeleteItems").Returns<int>();
                deleteContracts.CollectionParameter<long>("contractId");
                deleteContracts.Parameter<long>("projectId");
    
                builder.EntitySet<Contract>("Contracts");
                config.MapODataServiceRoute("r1", "r1", builder.GetEdmModel());
            }
        }
    
        public class Contract { public int Id { get; set; } }
    
        public class ContractsController : ODataController
        {
            [HttpPost]
            public IHttpActionResult DeleteItems([FromBody]ODataActionParameters param)
            {
                return Ok(2);
            }
        }
    }
    

    请求:

    POST http://localhost:57071/r1/Contracts/Default.DeleteItems HTTP/1.1
    Content-Type: application/json
    Host: localhost:57071
    Content-Length: 50
    
    {
      "contractId":[1,3,5,7,9],
      "projectId":9
    }
    

    响应:

    HTTP/1.1 200 OK
    Cache-Control: no-cache
    Pragma: no-cache
    Content-Type: application/json; odata.metadata=minimal
    Expires: -1
    Server: Microsoft-IIS/10.0
    OData-Version: 4.0
    Content-Length: 82
    
    {
      "@odata.context":"http://localhost:57071/r1/$metadata#Edm.Int32","value":2
    }
    

答案 1 :(得分:0)

Nuget无法正确解析oData dlls版本。在项目Odata版本的路由是v2 / 3,但实现是为新添加的控制器按照v4。