我很难从WCF数据服务迁移到web api odata v4。我坚持以下问题:
odata web api似乎不支持@ odata.bind。
我找到了以下链接:
https://github.com/OData/WebApi/issues/158
有人建议实现一个自己的ODataEntityDeserializer
派生类,如下所示:
public class ExtendedODataEntityDeserializer : ODataEntityDeserializer
{
public ExtendedODataEntityDeserializer(ODataDeserializerProvider deserializerProvider) : base(deserializerProvider)
{
}
public override void ApplyNavigationProperty(
object entityResource,
ODataNavigationLinkWithItems navigationLinkWrapper,
IEdmEntityTypeReference entityType,
ODataDeserializerContext readContext)
{
base.ApplyNavigationProperty(entityResource, navigationLinkWrapper, entityType, readContext);
foreach (var childItem in navigationLinkWrapper.NestedItems)
{
var entityReferenceLink = childItem as ODataEntityReferenceLinkBase;
if (entityReferenceLink != null)
{
var navigationPropertyName = navigationLinkWrapper.NavigationLink.Name;
Uri referencedEntityUrl = entityReferenceLink.EntityReferenceLink.Url;
if (!referencedEntityUrl.IsAbsoluteUri)
{
referencedEntityUrl = new Uri(readContext.Request.RequestUri, referencedEntityUrl);
}
var linkedEntities = (Model.LinkedEntityCollection)entityResource;
linkedEntities.Add(navigationPropertyName, referencedEntityUrl);
}
}
}
}
LinkedEntityCollection是我的实体类的基类,用作字典(MyEntity : LinkedEntityCollection
实现很简单)。
public class ExtendedODataDeserializerProvider : ODataDeserializerProvider
{
private static ExtendedODataDeserializerProvider _instance = null;
private ExtendedODataDeserializerProvider()
{
_instance = this;
}
public static ExtendedODataDeserializerProvider Instance
{
get { return _instance ?? new ExtendedODataDeserializerProvider(); }
}
public override ODataEdmTypeDeserializer GetEdmTypeDeserializer(Microsoft.OData.Edm.IEdmTypeReference edmType)
{
return DefaultODataDeserializerProvider.Instance.GetEdmTypeDeserializer(edmType);
}
public override ODataDeserializer GetODataDeserializer(Microsoft.OData.Edm.IEdmModel model, Type type, System.Net.Http.HttpRequestMessage request)
{
return new ExtendedODataEntityDeserializer(DefaultODataDeserializerProvider.Instance);
}
}
在http配置中注册:
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
// registering entities: builder.EntitySet<T>("EntityName") ...
ODataBatchHandler batchHandler = new DefaultODataBatchHandler(new HttpServer(config));
batchHandler.MessageQuotas.MaxOperationsPerChangeset = 10;
batchHandler.MessageQuotas.MaxPartsPerBatch = 10;
var odataFormatters = ODataMediaTypeFormatters.Create(DefaultODataSerializerProvider.Instance, ExtendedODataDeserializerProvider.Instance);
config.Formatters.Clear();
config.Formatters.AddRange(odataFormatters);
config.MapODataServiceRoute(routeName: "Central", routePrefix: "Odata", model: builder.GetEdmModel(), batchHandler: batchHandler);
}
但是,如果你确实使用了这个hack,你就失去了在这个aritcle中描述的enities之间创建链接的能力:http://www.asp.net/web-api/overview/odata-support-in-aspnet-web-api/odata-v4/entity-relations-in-odata-v4
“@ odata.id”未被解析,假设它是函数ODataEntityDeserializer.Read
中的实体。也许我在这里从根本上缺少了一些东西,因为我对web api很新(config.Formatters.Clear()
似乎有点苛刻)。或者还有其他解决方法来支持@ odata.bind吗?
这个功能是否有可能在不久的将来实施?
更新
我让我的黑客工作了。问题是我应该从ExtendedODataDeserializerProvider
而不是DefaultODataDeserializerProvider
中取代ODataDeserializerProvider
并相应地返回反序列化器:
public override ODataDeserializer GetODataDeserializer(
Microsoft.OData.Edm.IEdmModel model,
Type type,
System.Net.Http.HttpRequestMessage request)
{
if (type == typeof(ODataActionParameters) ||
type == typeof(ODataUntypedActionParameters) ||
type == typeof(Uri))
{
return base.GetODataDeserializer(model, type, request);
}
return new ExtendedODataEntityDeserializer(DefaultODataDeserializerProvider.Instance);
}
所以问题仍然存在:我们何时在asp.net webapi odata中获得@ odata.bind支持?
答案 0 :(得分:1)
尝试回答
所以问题仍然存在:我们何时在asp.net webapi odata中获得@ odata.bind支持?
在vnext repo中似乎有与此相关的功能。但是,该问题标志着影响较小。 https://github.com/OData/WebApi/milestones/vNext因此无法知道何时实施此功能。