Asp.net mvc ASP.NET MVC中的RSS源

Asp.net mvc ASP.NET MVC中的RSS源,asp.net-mvc,rss,Asp.net Mvc,Rss,如何在ASP.NET MVC中处理RSS源?使用第三方库?在BCL中使用RSS内容?只是创建一个呈现XML的RSS视图?还是完全不同的东西?以下是我的建议: 创建一个名为RssResult的类 继承抽象基类 行动结果 重写ExecuteSult方法 ExecuteResult由调用者将ControllerContext传递给它,通过它您可以获得数据和内容类型 将内容类型更改为rss后,您将希望将数据序列化为rss(使用您自己的代码或其他库)并写入响应 在要返回rss的控制器上创建操作,并将返回类

如何在ASP.NET MVC中处理RSS源?使用第三方库?在BCL中使用RSS内容?只是创建一个呈现XML的RSS视图?还是完全不同的东西?

以下是我的建议:

  • 创建一个名为RssResult的类 继承抽象基类 行动结果
  • 重写ExecuteSult方法
  • ExecuteResult由调用者将ControllerContext传递给它,通过它您可以获得数据和内容类型
  • 将内容类型更改为rss后,您将希望将数据序列化为rss(使用您自己的代码或其他库)并写入响应

  • 在要返回rss的控制器上创建操作,并将返回类型设置为RssResult。根据要返回的内容从模型中获取数据

  • 然后,对该操作的任何请求都将收到您选择的任何数据的rss


  • 在ASP.NET MVC中,这可能是返回rss的最快和可重用的方法,它可以响应请求。

    另一种疯狂的方法,但有其优点,就是使用普通的.aspx视图来呈现rss。在操作方法中,只需设置适当的内容类型。这种方法的一个好处是很容易理解渲染的内容以及如何添加自定义元素,如地理位置


    再说一次,列出的其他方法可能更好,我只是没有使用它们

    我同意哈克德的观点。我目前正在使用MVC框架实现我的站点/博客,我采用了创建RSS新视图的简单方法:

    <%@ Page ContentType="application/rss+xml" Language="C#" AutoEventWireup="true" CodeBehind="PostRSS.aspx.cs" Inherits="rr.web.Views.Blog.PostRSS" %><?xml version="1.0" encoding="utf-8"?>
    <rss version="2.0">
    <channel>
    <title>ricky rosario's blog</title>
    <link>http://<%= Request.Url.Host %></link>
    <description>Blog RSS feed for rickyrosario.com</description>
    <lastBuildDate><%= ViewData.Model.First().DatePublished.Value.ToUniversalTime().ToString("r") %></lastBuildDate>
    <language>en-us</language>
    <% foreach (Post p in ViewData.Model) { %>
        <item>
        <title><%= Html.Encode(p.Title) %></title>
        <link>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></link>
        <guid>http://<%= Request.Url.Host + Url.Action("ViewPostByName", new RouteValueDictionary(new { name = p.Name })) %></guid>
        <pubDate><%= p.DatePublished.Value.ToUniversalTime().ToString("r") %></pubDate>
        <description><%= Html.Encode(p.Content) %></description>
        </item>
    <% } %>
    </channel>
    </rss>
    
    
    里基·罗萨里奥的博客
    http://
    rickyrosario.com的博客RSS源
    美国英语
    http://
    http://
    

    有关更多信息,请查看(无耻的插件)

    该.NET framework公开了处理联合的类:SyndicationFeed等。 因此,与其自己进行渲染,或者使用其他建议的RSS库,为什么不让框架来处理呢

    基本上,您只需要以下自定义ActionResult,就可以开始了:

    public class RssActionResult : ActionResult
    {
        public SyndicationFeed Feed { get; set; }
    
        public override void ExecuteResult(ControllerContext context)
        {
            context.HttpContext.Response.ContentType = "application/rss+xml";
    
            Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
            using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
            {
                rssFormatter.WriteTo(writer);
            }
        }
    }
    
    现在,在控制器操作中,您可以简单地返回以下内容:

    return new RssActionResult() { Feed = myFeedInstance };
    

    在我的博客上有一个完整的示例,我从Eran Kampf和Scott Hanselman vid(忘记了链接)那里得到了这个示例,所以它与这里的一些其他帖子略有不同,但希望有帮助,可以作为rss提要的一个示例复制粘贴

    使用系统;
    使用System.Collections.Generic;
    使用System.ServiceModel.Syndication;
    使用System.Web;
    使用System.Web.Mvc;
    使用System.Xml;
    命名空间MVC3JavaScript_3_2012.Rss
    {
    公共类RssFeed:FileResult
    {
    私有Uri _currentUrl;
    私有只读字符串_title;
    私有只读字符串\u描述;
    私有只读列表项;
    公共RssFeed(字符串内容类型、字符串标题、字符串描述、列表项)
    :base(contentType)
    {
    _头衔=头衔;
    _描述=描述;
    _项目=项目;
    }
    受保护的重写无效写文件(HttpResponseBase响应)
    {
    var feed=new SyndicationFeed(标题:this.\u标题,描述:\u描述,feedAlternateLink:\u当前URL,
    项目:此项。_项);
    var格式化程序=新的Rss20FeedFormatter(提要);
    使用(var writer=XmlWriter.Create(response.Output))
    {
    格式化程序。WriteTo(writer);
    }
    }
    公共覆盖无效ExecuteSult(ControllerContext上下文)
    {
    _currentUrl=context.RequestContext.HttpContext.Request.Url;
    base.executesult(上下文);
    }
    }
    }
    
    控制器代码

        [HttpGet]
    public ActionResult RssFeed()
    {
        var items = new List<SyndicationItem>();
        for (int i = 0; i < 20; i++)
        {
            var item = new SyndicationItem()
            {
                Id = Guid.NewGuid().ToString(),
                Title = SyndicationContent.CreatePlaintextContent(String.Format("My Title {0}", Guid.NewGuid())),
                Content = SyndicationContent.CreateHtmlContent("Content The stuff."),
                PublishDate = DateTime.Now
            };
            item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri("http://www.google.com")));//Nothing alternate about it. It is the MAIN link for the item.
            items.Add(item);
        }
    
        return new RssFeed(title: "Greatness",
                           items: items,
                           contentType: "application/rss+xml",
                           description: String.Format("Sooper Dooper {0}", Guid.NewGuid()));
    
    }
    
    [HttpGet]
    公共行动结果RssFeed()
    {
    var items=新列表();
    对于(int i=0;i<20;i++)
    {
    var item=new SyndicationItem()
    {
    Id=Guid.NewGuid().ToString(),
    Title=SyndicationContent.CreatePlaintextContent(String.Format(“我的标题{0}”,Guid.NewGuid()),
    Content=SyndicationContent.CreateHtmlContent(“为素材添加内容”),
    PublishDate=DateTime.Now
    };
    item.Links.Add(SyndicationLink.CreateAlternateLink)(新Uri(“http://www.google.com);//没有任何其他选项。它是该项的主链接。
    项目。添加(项目);
    }
    返回新的RssFeed(标题:“伟大”,
    项目:项目,,
    contentType:“应用程序/rss+xml”,
    description:String.Format(“Sooper Dooper{0}”,Guid.NewGuid());
    }
    
    Hanselman有一个(视频:大约从41m开始)从FileResult继承。通过这样做,您可以让类的构造函数调用
    base(“application/rss+xml”)
    并避免步骤3和步骤4。他确实覆盖了ExecuteSult,但这并不重要。他还简化了许多典型的朴素代码,并使用了
    辛迪加项目
    辛迪加提要
    、和
    Rss20FeedFormatter
    @Dale的3.5+功能:当您想要输出到部分视图时,是否可以写入响应?谢谢。更新了我先前评论中的链接。对于Razor的使用:@model PageModel@{Response.ContentType=“application/rss+xml”}什么开销?你的意思是,为了以更可读的方式完成同样的事情,你正在编写更少的代码?@Haacked:这个世界充满了由模板系统生成的无效rssxml。请不要把事情搞得一团糟!瑞奇,HTML编码!=XML编码。下面是来自MSDN的Html编码文档:>由于当前的实现细节,此函数可以用作xmlEncode函数。目前,此函数使用的所有命名实体也是xml预定义的命名实体。它们是<>“&编码为”和&;。其他实体采用十进制编码,如 ;。如何确保XML是有效的
        [HttpGet]
    public ActionResult RssFeed()
    {
        var items = new List<SyndicationItem>();
        for (int i = 0; i < 20; i++)
        {
            var item = new SyndicationItem()
            {
                Id = Guid.NewGuid().ToString(),
                Title = SyndicationContent.CreatePlaintextContent(String.Format("My Title {0}", Guid.NewGuid())),
                Content = SyndicationContent.CreateHtmlContent("Content The stuff."),
                PublishDate = DateTime.Now
            };
            item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri("http://www.google.com")));//Nothing alternate about it. It is the MAIN link for the item.
            items.Add(item);
        }
    
        return new RssFeed(title: "Greatness",
                           items: items,
                           contentType: "application/rss+xml",
                           description: String.Format("Sooper Dooper {0}", Guid.NewGuid()));
    
    }