6

我正在寻找通过 MVC4(和/或 WebAPI)创建 RSS 提要的最佳方式。这篇文章似乎最适用http://www.strathweb.com/2012/04/rss-atom-mediatypeformatter-for-asp-net-webapi/。但它是在 WebAPI 发布前的日子里写的。我已经使用 Nuget 来更新所有包,但尝试构建项目时会折腾:

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 自 beta 以来发生了显着变化,但我发现了有关代码片段所需调整的详细信息。

是否有显示 RSSFormatter 构造的更新资源?

谢谢

4

1 回答 1

9

是的,我针对 Beta 编写了该教程。

下面是更新到 RTM 版本的代码。

如果可以的话,一个建议是,这个示例使用了一个简单的具体类型的“白名单”,为它构建了 RSS/Atom 提要(在本例中是我的Url模型)。理想情况下,在更复杂的场景中,您应该针对接口而不是具体类型设置格式化程序,并让所有应该作为 RSS 公开的模型来实现该接口。

希望这可以帮助。

   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;
        }
    }
于 2012-09-15T14:34:34.667 回答