2012-09-15 40 views
5

Tôi đang tìm cách tốt nhất để tạo nguồn cấp dữ liệu RSS qua MVC4 (và/hoặc WebAPI). Bài đăng này có vẻ là áp dụng nhất http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/. Nhưng nó được viết trong những ngày trước khi phát hành của WebAPI. Tôi đã sử dụng NuGet để mang lại tất cả các gói up-to-date nhưng cố gắng để xây dựng dự án tung:Tạo nguồn cấp dữ liệu RSS trong 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 

Tôi đã tìm thấy một số điều giải thích rằng MediaTypeFormatter đã thay đổi đáng kể từ khi phiên bản beta nhưng tôi đã tìm thấy chi tiết về các điều chỉnh bắt buộc đối với đoạn mã được đề cập.

Có tài nguyên được cập nhật nào hiển thị việc xây dựng một RSSFormatter không?

thx

Trả lời

8

Vâng tôi đã viết hướng dẫn đó về Beta.

Dưới đây là mã được cập nhật lên phiên bản RTM.

Một lời khuyên, nếu có thể, là ví dụ này sử dụng "danh sách trắng" đơn giản của các loại cụ thể mà nguồn cấp dữ liệu RSS/Atom đang tạo (trong trường hợp này là kiểu Url). Lý tưởng nhất trong các kịch bản phức tạp hơn, bạn có trình định dạng được thiết lập dựa trên một giao diện, chứ không phải là một loại cụ thể và có tất cả các Mô hình được cho là được hiển thị dưới dạng RSS để triển khai giao diện đó.

Hy vọng điều này sẽ hữu ích.

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

Hãy suy nghĩ về việc triển khai điều này cho api web của tôi. Một điều mà Im đang tìm kiếm để làm điều này là tạo một thuộc tính cung cấp để tôi có thể đánh dấu các lớp của mình với tiêu đề… –

+0

Bất kỳ ý tưởng nào để làm điều này với các bit aspvnext? –

+0

không phù hợp với tất cả, aspnetvnext sẽ hoàn toàn độc lập với API Web hiện tại hoặc MVC –

Các vấn đề liên quan