Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes I wrote that tutorial against Beta.</p> <p>Below is the code updated to RTM version.</p> <p>One advice, if I may, is that this example uses a simple "whitelist" of concrete types for which RSS/Atom feed is build (in this case my <code>Url</code> model). Ideally in more complex scenarios, you'd have the formatter set up against an interface, rather than a concrete type, and have all Models which are supposed to be exposed as RSS to implement that interface.</p> <p>Hope this helps.</p> <pre><code> 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&lt;Type, bool&gt; SupportedType = (type) =&gt; { if (type == typeof(Url) || type == typeof(IEnumerable&lt;Url&gt;)) 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(() =&gt; { if (type == typeof(Url) || type == typeof(IEnumerable&lt;Url&gt;)) BuildSyndicationFeed(value, writeStream, content.Headers.ContentType.MediaType); }); } private void BuildSyndicationFeed(object models, Stream stream, string contenttype) { List&lt;SyndicationItem&gt; items = new List&lt;SyndicationItem&gt;(); var feed = new SyndicationFeed() { Title = new TextSyndicationContent("My Feed") }; if (models is IEnumerable&lt;Url&gt;) { var enumerator = ((IEnumerable&lt;Url&gt;)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; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload