使用$ expand选项

时间:2016-03-03 20:43:38

标签: asp.net-web-api-odata odata-v4

对OData服务的请求类似于下面的请求按预期工作:

http://localhost:31054/odata/Articles $滤波器= ArticleRatings /任何()&安培; $的OrderBy =条款ArticleID%20desc&安培; $扩大=出版物&安培; $格式= JSON

但是,当我将格式更改为xml时,相同的请求将失败,并显示以下错误消息:

<Error>
    <Message>An error has occurred.</Message>
    <ExceptionMessage>
        The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
    </ExceptionMessage>
    <ExceptionType>System.InvalidOperationException</ExceptionType>
    <StackTrace/>
    <InnerException>
        <Message>An error has occurred.</Message>
        <ExceptionMessage>
            Object of type 'System.Linq.EnumerableQuery`1[System.Web.OData.Query.Expressions.SelectExpandBinder+SelectAllAndExpand`1[DataAccess.OData.Article]]' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[DataAccess.OData.Article]'.
        </ExceptionMessage>
        <ExceptionType>System.ArgumentException</ExceptionType>
        <StackTrace>
            at System.RuntimeType.TryChangeType(Object value, Binder binder, CultureInfo culture, Boolean needsSpecialCast) at System.RuntimeType.CheckValue(Object value, Binder binder, CultureInfo culture, BindingFlags invokeAttr) at System.Reflection.MethodBase.CheckArguments(Object[] parameters, Binder binder, BindingFlags invokeAttr, CultureInfo culture, Signature sig) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content) at System.Net.Http.Formatting.XmlMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken) --- 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__1b.MoveNext()
        </StackTrace>
    </InnerException>
</Error>

毋庸置疑,当不使用 $ expand 选项时,两种格式都可以正常工作。

我正在使用Web Api OData的最新稳定版本: Microsoft.OData.Core&#34;版本6.14.0 Microsoft.AspNet.OData版本5.8.0

有任何想法如何解决此问题?

0 个答案:

没有答案