2012-09-15 16 views
5

में आरएसएस फ़ीड बनाएं मैं एमवीसी 4 (और/या वेबएपीआई) के माध्यम से आरएसएस फ़ीड बनाने का सबसे अच्छा तरीका ढूंढ रहा हूं। यह पोस्ट सबसे लागू http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/ लग रहा था। लेकिन यह वेबएपीआई के पूर्व-रिलीज दिनों में लिखा गया था। मैं Nuget का उपयोग किया है सभी संकुल लाने के लिए अप करने की तारीख, लेकिन परियोजना बनाने का प्रयास tosses:एमवीसी 4/वेबएपीआई

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 

मैं समझा कि MediaTypeFormatter बीटा के बाद से काफी बदल गया है लेख के एक नंबर मिल गया है, लेकिन मैं पाया है प्रश्न में कोड स्निपेट के लिए आवश्यक समायोजन पर विवरण।

क्या कोई अपडेट किया गया संसाधन है जो आरएसएसफ़ॉर्मेटर का निर्माण दिखा रहा है?

thx

उत्तर

8

हाँ मैंने बीटा के खिलाफ ट्यूटोरियल लिखा था।

नीचे कोड आरटीएम संस्करण में अद्यतन किया गया है।

एक सलाह, यदि मैं कर सकता हूं, यह है कि यह उदाहरण कंक्रीट प्रकारों के एक सरल "श्वेतसूची" का उपयोग करता है जिसके लिए आरएसएस/एटम फ़ीड बनती है (इस मामले में मेरा Url मॉडल)। आदर्श रूप से अधिक जटिल परिदृश्यों में, आपके पास एक ठोस प्रकार की बजाय एक इंटरफ़ेस के खिलाफ फ़ॉर्मेटर स्थापित होगा, और उस मॉडल को लागू करने के लिए आरएसएस के रूप में उजागर किए जाने वाले सभी मॉडल हैं।

उम्मीद है कि इससे मदद मिलती है।

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

मैं अपने वेब एपीआई के लिए इसे लागू करने के बारे में सोच रहा हूं। एक चीज जो मैं इस पर शीर्ष पर दिखाना चाहता हूं वह सिंडिकेशन विशेषता बनाना है ताकि मैं अपने वर्गों को शीर्षक आदि के साथ चिह्नित कर सकूं। –

+0

कोई विचार एस्पनेक्स्ट बिट्स के साथ ऐसा कैसे करें? –

+0

बिल्कुल फिट नहीं है, aspnetvnext वर्तमान वेब एपीआई या वर्तमान एमवीसी –