2012-09-15 25 views
5

Sto cercando il modo migliore per creare un feed RSS tramite MVC4 (e/o WebAPI). Questo post è sembrato il più applicabile http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/. Ma è stato scritto nei giorni precedenti alla pubblicazione di WebAPI. Ho usato Nuget per portare tutti i pacchetti up-to-date, ma il tentativo di costruire il progetto getta:Creare feed RSS in MVC4/WebAPI

Error 2 The type or namespace name 'FormatterContext' could not be found (are you missing a using directive or an assembly reference?) G:\Code\MvcApplication-atomFormatter\MvcApplication-atomFormatter\SyndicationFeedFormatter.cs 38 129 MvcApplication_syndicationFeedFormatter 

Ho trovato un certo numero di articoli che spiegano che il MediaTypeFormatter è cambiata in modo significativo dal beta, ma ho trovato dettagli sugli aggiustamenti richiesti per lo snippet di codice in questione.

Esiste una risorsa aggiornata che mostra la costruzione di un RSSFormatter?

thx

risposta

8

Sì ho scritto che un'esercitazione contro Beta.

Di seguito è riportato il codice aggiornato alla versione RTM.

Un consiglio, se possibile, è che in questo esempio viene utilizzata una semplice "lista bianca" di tipi concreti per i quali viene creato il feed RSS/Atom (in questo caso il mio modello Url). Idealmente in scenari più complessi, avresti il ​​formattatore impostato su un'interfaccia, piuttosto che un tipo concreto, e hai tutti i Modelli che dovrebbero essere esposti come RSS per implementare quell'interfaccia.

Spero che questo aiuti.

public class SyndicationFeedFormatter : MediaTypeFormatter 
    { 
     private readonly string atom = "application/atom+xml"; 
     private readonly string rss = "application/rss+xml"; 

     public SyndicationFeedFormatter() 
     { 
      SupportedMediaTypes.Add(new MediaTypeHeaderValue(atom)); 
      SupportedMediaTypes.Add(new MediaTypeHeaderValue(rss)); 
     } 

     Func<Type, bool> SupportedType = (type) => 
     { 
      if (type == typeof(Url) || type == typeof(IEnumerable<Url>)) 
       return true; 
      else 
       return false; 
     }; 

     public override bool CanReadType(Type type) 
     { 
      return SupportedType(type); 
     } 

     public override bool CanWriteType(Type type) 
     { 
      return SupportedType(type); 
     } 

     public override Task WriteToStreamAsync(Type type, object value, Stream writeStream, System.Net.Http.HttpContent content, System.Net.TransportContext transportContext) 
     { 
      return Task.Factory.StartNew(() => 
      { 
       if (type == typeof(Url) || type == typeof(IEnumerable<Url>)) 
        BuildSyndicationFeed(value, writeStream, content.Headers.ContentType.MediaType); 
      }); 
     } 

     private void BuildSyndicationFeed(object models, Stream stream, string contenttype) 
     { 
      List<SyndicationItem> items = new List<SyndicationItem>(); 
      var feed = new SyndicationFeed() 
      { 
       Title = new TextSyndicationContent("My Feed") 
      }; 

      if (models is IEnumerable<Url>) 
      { 
       var enumerator = ((IEnumerable<Url>)models).GetEnumerator(); 
       while (enumerator.MoveNext()) 
       { 
        items.Add(BuildSyndicationItem(enumerator.Current)); 
       } 
      } 
      else 
      { 
       items.Add(BuildSyndicationItem((Url)models)); 
      } 

      feed.Items = items; 

      using (XmlWriter writer = XmlWriter.Create(stream)) 
      { 
       if (string.Equals(contenttype, atom)) 
       { 
        Atom10FeedFormatter atomformatter = new Atom10FeedFormatter(feed); 
        atomformatter.WriteTo(writer); 
       } 
       else 
       { 
        Rss20FeedFormatter rssformatter = new Rss20FeedFormatter(feed); 
        rssformatter.WriteTo(writer); 
       } 
      } 
     } 

     private SyndicationItem BuildSyndicationItem(Url u) 
     { 
      var item = new SyndicationItem() 
      { 
       Title = new TextSyndicationContent(u.Title), 
       BaseUri = new Uri(u.Address), 
       LastUpdatedTime = u.CreatedAt, 
       Content = new TextSyndicationContent(u.Description) 
      }; 
      item.Authors.Add(new SyndicationPerson() { Name = u.CreatedBy }); 
      return item; 
     } 
    } 
+0

Sto pensando di implementare questo per il mio web api. L'unica cosa che sto cercando di fare sopra è creare un attributo di syndication in modo da poter contrassegnare le mie classi con il titolo ecc. –

+0

Qualche idea su come fare questo con i bit di aspvnext? –

+0

non si adatta affatto, aspnetvnext sarà completamente indipendente dalle attuali API Web o MVC attuale –