WebAPI OData错误ObjectContent类型无法序列化内容类型'application / json ...'的响应正文

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WebAPI OData错误ObjectContent类型无法序列化内容类型'application / json ...'的响应正文相关的知识,希望对你有一定的参考价值。

这个是杀了我。这里的文章和网络都没有帮助。

首先,我正在使用.Net 4.5开发ASP.Net WebForms(非MVC)。我找到了一个great article,可以帮助您向WebForms站点添加OData源。它像一个冠军一样工作。我能够创建一个EMPTY Web应用程序并使其工作。但是,我注意到它没有使用我通过EntitySetController创建的最新(并且据说更容易)this article。两者都分开工作。然后我按摩原始文章,看看它是否可以处理EntitySetController,它可以。使用Fiddler建议测试OData及其过滤。哦,快乐的一天。

我的下一步是将它添加到我现有的ASP.Net 4.5 WebForms应用程序中。有点工作了。一切都编译,我可以打电话给locallhost:55777/kid,它按预期返回Products

<workspace>
  <atom:title type="text">Default</atom:title>
  <collection href="Products">
    <atom:title type="text">Products</atom:title>
  </collection>
</workspace>

然后我尝试GetGetEntityByKey方法,他们跑,并回馈他们应该做的。但是,我收到以下错误消息:

{
  "odata.error":{
    "code":"","message":{
      "lang":"en-US","value":"An error has occurred."
    },
    "innererror":{
      "message":"The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; odata=minimalmetadata; streaming=true; charset=utf-8'.",
      "type":"System.InvalidOperationException",
      "stacktrace":"",
      "internalexception":{
        "message":"No IdLink factory was found. Try calling HasIdLink on the EntitySetConfiguration for 'Products'.",
        "type":"System.InvalidOperationException",
        "stacktrace":"   at System.Web.Http.OData.Builder.EntitySetLinkBuilderAnnotation.BuildIdLink(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel)

                         at System.Web.Http.OData.Builder.EntitySetLinkBuilderAnnotation.BuildEntitySelfLinks(EntityInstanceContext instanceContext, ODataMetadataLevel metadataLevel)
   
                         at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.CreateEntry(SelectExpandNode selectExpandNode, EntityInstanceContext entityInstanceContext)
   
                         at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteEntry(Object graph, ODataWriter writer, ODataSerializerContext writeContext)
   
                         at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObjectInline(Object graph, IEdmTypeReference expectedType, ODataWriter writer, ODataSerializerContext writeContext)

                         at System.Web.Http.OData.Formatter.Serialization.ODataEntityTypeSerializer.WriteObject(Object graph, Type type, ODataMessageWriter messageWriter, ODataSerializerContext writeContext)
   
                         at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content, HttpContentHeaders contentHeaders)
   
                         at System.Web.Http.OData.Formatter.ODataMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext)
--- End of stack trace from previous location where exception was thrown ---
   
                         at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)

                         at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   
                         at System.Web.Http.WebHost.HttpControllerHandler.<WriteBufferedResponseContentAsync>d__10.MoveNext()"
      }
    }
  }
}

控制器是:

using System.Collections.Generic;
using System.Linq;
using System.Web.Http.OData;

namespace BOR.InternalWebsite.Controllers {

    public class ProductsController : EntitySetController<Product, int> {

        static List<Product> products = new List<Product>() {
            new Product() { ID = 1, Name = "Hat", Price = 15, Category = "Apparel" },
            new Product() { ID = 2, Name = "Socks", Price = 5, Category = "Apparel" },
            new Product() { ID = 3, Name = "Scarf", Price = 12, Category = "Apparel" },
            new Product() { ID = 4, Name = "Yo-yo", Price = 4.95M, Category = "Toys" },
            new Product() { ID = 5, Name = "Puzzle", Price = 8, Category = "Toys" },
        };

        public override IQueryable<Product> Get() {
            return products.AsQueryable();
        }

        protected override Product GetEntityByKey(int key) {
            return products.FirstOrDefault(p => p.ID == key);
        }

    }
}

WebApiConfig是:

using Microsoft.Data.Edm;
using System.Web.Http;
using System.Web.Http.OData.Builder;

namespace BOR.InternalWebsite {

    public static class WebApiConfig {

        public static void Register(HttpConfiguration config) {
            config.EnableQuerySupport();

            ODataModelBuilder modelBuilder = new ODataModelBuilder();
            var products = modelBuilder.EntitySet<Product>("Products");

            IEdmModel model = modelBuilder.GetEdmModel();
            config.Routes.MapODataRoute("ODataRoute", "kid", model);
        }

    }
}

Global.asax.cs文件的Application_Start除了以下内容之外什么都没有:

WebApiConfig.Register(GlobalConfiguration.Configuration);

只是为了向您展示我在项目中有哪些包,这是我的Packages.config文件。我知道Microsoft.AspNet.WebApi.*项目是预先发布的。我在目前的稳定版本中使用过它们并没有什么不同所以我想我会试着看看预发布版是否可以修复它。

<?xml version="1.0" encoding="utf-8"?>
<packages>
    <package id="DynamicDataTemplatesCS" version="1.0.1" targetFramework="net45" />
    <package id="elmah" version="1.2.2" targetFramework="net45" />
    <package id="elmah.corelibrary" version="1.2.2" targetFramework="net45" />
    <package id="EntityFramework" version="5.0.0" targetFramework="net45" />
    <package id="jQuery" version="2.0.3" targetFramework="net45" />
    <package id="jquery.mobile" version="1.3.2" targetFramework="net45" />
    <package id="jQuery.UI.Combined" version="1.10.3" targetFramework="net45" />
    <package id="knockoutjs" version="2.3.0" targetFramework="net45" />
    <package id="Microsoft.AspNet.WebApi" version="5.0.0-rc1" targetFramework="net45" />
    <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0-rc1" targetFramework="net45" />
    <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0-rc1" targetFramework="net45" />
    <package id="Microsoft.AspNet.WebApi.OData" version="5.0.0-rc1" targetFramework="net45" />
    <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0-rc1" targetFramework="net45" />
    <package id="Microsoft.Bcl" version="1.1.3" targetFramework="net45" />
    <package id="Microsoft.Bcl.Build" version="1.0.10" targetFramework="net45" />
    <package id="Microsoft.Data.Edm" version="5.6.0" targetFramework="net45" />
    <package id="Microsoft.Data.OData" version="5.6.0" targetFramework="net45" />
    <package id="Microsoft.Net.Http" version="2.2.15" targetFramework="net45" />
    <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net45" />
    <package id="System.Spatial" version="5.6.0" targetFramework="net45" />
    <package id="Twitter.Bootstrap" version="3.0.0" targetFramework="net45" />
</packages>

由于我可以独立工作,我觉得有些事情正在阻碍。任何有用的帮助将不胜感激! TIA!

编辑==解决方案

感谢@RaghuRam,唯一需要改变的是WebApiConfig的Register方法。更新后的工作版本如下:

config.EnableQuerySupport();

ODataConventionModelBuilder modelBuilder = new ODataConventionModelBuilder();
var products = modelBuilder.EntitySet<Product>("Products");

IEdmModel model = modelBuilder.GetEdmModel();
config.Routes.MapODataRoute("ODataRoute", "kid", model);

真棒!

答案

而不是ODataModelBuilder,像这样使用ODataConventionModelBuilder,

var modelBuilder = new ODataConventionModelBuilder ();
var products = modelBuilder.EntitySet<Product>("Products");

IEdmModel model = modelBuilder.GetEdmModel();

ODataModelBuilder是一个非常低级的类,旨在当您想要明确配置整个模型时使用。您需要告诉它每个属性,每个导航属性,然后是自我链接(id,编辑和读取)和导航链接。

另一方面,ODataConventionModelBuilder有一些约定可以自动推断这些事情。只有在偏离约定时才需要使用ODataConventionModelBuilder显式配置。

如果你仍然希望使用ODataModelBuilder类,你应该编写这样的代码,

ODataModelBuilder modelBuilder = new ODataModelBuilder();
var products = modelBuilder.EntitySet<Product>("Products");
var product = products.EntityType;

product.HasKey(p => p.ID);
product.Property(p => p.Name);
product.Property(p => p.Price);
product.Property(p => p.Category);

products.HasIdLink((ctxt) => ctxt.Url.ODataLink(new EntitySetPathSegment("Products"), new KeyValuePathSegment(ctxt.EntityInstance.ID)));

IEdmModel model = modelBuilder.GetEdmModel();

一旦有了导航属性和相关的实体集等,事情会变得更加复杂。

以上是关于WebAPI OData错误ObjectContent类型无法序列化内容类型'application / json ...'的响应正文的主要内容,如果未能解决你的问题,请参考以下文章

武装你的WEBAPI-OData聚合查询

武装你的WEBAPI-OData便捷查询

武装你的WEBAPI-OData便捷查询

武装你的WEBAPI-OData入门

武装你的WEBAPI-OData入门

武装你的WEBAPI-OData资源更新Delta