customizing odata output from asp.net web api

前端 未结 1 1751
逝去的感伤
逝去的感伤 2020-12-06 12:14

I\'m using the new ASP.NET webapi odata (version 4.0.0 last published 27/2/2013 according to Nuget)

Basically I\'m doing it as described here: http://www.asp.net/web

相关标签:
1条回答
  • 2020-12-06 12:41

    The OData media type formatter is more extensible now. Samples follow.

    1) decide what goes on the title, author and updated elements for the feed

    public class AtomMetadataFeedSerializer : ODataFeedSerializer
    {
        public AtomMetadataFeedSerializer(IEdmCollectionTypeReference edmType, ODataSerializerProvider serializerProvider)
            : base(edmType, serializerProvider)
        {
        }
    
        public override ODataFeed CreateODataFeed(IEnumerable feedInstance, ODataSerializerContext writeContext)
        {
            ODataFeed feed = base.CreateODataFeed(feedInstance, writeContext);
            feed.Atom().Title = new AtomTextConstruct { Kind = AtomTextConstructKind.Text, Text = "My Awesome Feed" };
            return feed;
        }
    }
    
    public class CustomSerializerProvider : DefaultODataSerializerProvider
    {
        public override ODataEntrySerializer CreateEdmTypeSerializer(IEdmTypeReference edmType)
        {
            if (edmType.IsCollection() && edmType.AsCollection().ElementType().IsEntity())
            {
                // feed serializer
                return new AtomMetadataFeedSerializer(edmType.AsCollection(), this);
            }
    
            return base.CreateEdmTypeSerializer(edmType);
        }
    }
    

    And register the custom serializer provider using,

    config.Formatters.InsertRange(0, ODataMediaTypeFormatters.Create(new CustomSerializerProvider(), new DefaultODataDeserializerProvider()));
    

    2) customize edit links

    public class CustomEntityTypeSerializer : ODataEntityTypeSerializer
    {
        public CustomEntityTypeSerializer(IEdmEntityTypeReference edmType, ODataSerializerProvider serializerProvider)
            : base(edmType, serializerProvider)
        {
        }
    
        public override ODataEntry CreateEntry(EntityInstanceContext entityInstanceContext, ODataSerializerContext writeContext)
        {
            ODataEntry entry = base.CreateEntry(entityInstanceContext, writeContext);
            if (notProduceEditLinks)
            {
                entry.EditLink = null;
            }
    
            return entry;
        }
    }
    
    public class CustomSerializerProvider : DefaultODataSerializerProvider
    {
        public override ODataEntrySerializer CreateEdmTypeSerializer(IEdmTypeReference edmType)
        {
            if (edmType.IsEntity())
            {
                // entity type serializer
                return new CustomEntityTypeSerializer(edmType.AsEntity(), this);
            }
    
            return base.CreateEdmTypeSerializer(edmType);
        }
    }
    

    and register the custom serializer provider as above.

    We still don't support scenario 3 i.e aliasing type names and namespaces.

    0 讨论(0)
提交回复
热议问题