【问题标题】:Easy Way to Consume/Display RSS Feed in MVC ASP.NET在 MVC ASP.NET 中使用/显示 RSS 提要的简单方法
【发布时间】:2010-09-21 05:25:37
【问题描述】:

我是 MVC 框架的新手,想知道如何将 RSS 数据从控制器传递到视图。我知道需要转换为某种类型的 IEnumerable 列表。我已经看到了一些创建匿名类型的示例,但无法弄清楚如何将 RSS 提要转换为通用列表并将其传递给视图。

我也不希望它是强类型的,因为会多次调用各种 RSS 提要。

任何建议。

【问题讨论】:

    标签: asp.net-mvc model-view-controller


    【解决方案1】:

    rss 是一个特殊格式的 xml 文件。您可以设计具有该通用格式的数据集,并使用 ReadXml 方法读取 rss(xml) 并将 uri 作为文件的路径。然后你就有了一个可以从其他类中使用的数据集。

    【讨论】:

      【解决方案2】:

      我一直在尝试一种在 MVC 中执行 WebPart 的方法,它基本上是包装在 webPart 容器中的 UserControl。我的一个测试 UserControls 是一个 Rss Feed 控件。我使用 Futures dll 中的 RenderAction HtmlHelper 扩展来显示它,以便调用控制器操作。我使用 SyndicationFeed 类来完成大部分工作

      using (XmlReader reader = XmlReader.Create(feed))
      {
          SyndicationFeed rssData = SyndicationFeed.Load(reader);
      
          return View(rssData);
       }
      

      下面是控制器和UserControl的代码:

      控制器代码为:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      using System.Web.Mvc.Ajax;
      using System.Xml;
      using System.ServiceModel.Syndication;
      using System.Security;
      using System.IO;
      
      namespace MvcWidgets.Controllers
      {
          public class RssWidgetController : Controller
          {
              public ActionResult Index(string feed)
              {
                  string errorString = "";
      
                  try
                  {
                      if (String.IsNullOrEmpty(feed))
                      {
                          throw new ArgumentNullException("feed");
                      }
                          **using (XmlReader reader = XmlReader.Create(feed))
                          {
                              SyndicationFeed rssData = SyndicationFeed.Load(reader);
      
                              return View(rssData);
                          }**
                  }
                  catch (ArgumentNullException)
                  {
                      errorString = "No url for Rss feed specified.";
                  }
                  catch (SecurityException)
                  {
                      errorString = "You do not have permission to access the specified Rss feed.";
                  }
                  catch (FileNotFoundException)
                  {
                      errorString = "The Rss feed was not found.";
                  }
                  catch (UriFormatException)
                  {
                      errorString = "The Rss feed specified was not a valid URI.";
                  }
                  catch (Exception)
                  {
                      errorString = "An error occured accessing the RSS feed.";
                  }
      
                  var errorResult = new ContentResult();
                  errorResult.Content = errorString;
                  return errorResult;
      
              }
          }
      }
      

      用户控件

      <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="Index.ascx.cs" Inherits="MvcWidgets.Views.RssWidget.Index" %>
      <div class="RssFeedTitle"><%= Html.Encode(ViewData.Model.Title.Text) %> &nbsp; <%= Html.Encode(ViewData.Model.LastUpdatedTime.ToString("MMM dd, yyyy hh:mm:ss") )%></div>
      
      <div class='RssContent'>
      <% foreach (var item in ViewData.Model.Items)
         {
             string url = item.Links[0].Uri.OriginalString;
             %>
         <p><a href='<%=  url %>'><b> <%= item.Title.Text%></b></a>
         <%  if (item.Summary != null)
             {%>
              <br/> <%= item.Summary.Text %>
          <% }
         } %> </p>
      </div>
      

      将后面的代码修改为具有类型化模型

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.Web.Mvc;
      using System.ServiceModel.Syndication;
      
      namespace MvcWidgets.Views.RssWidget
      {
          public partial class Index : System.Web.Mvc.ViewUserControl<SyndicationFeed>
          {
          }
      }
      

      【讨论】:

      • 我很高兴它有帮助。你的申请顺利吗?
      • 几个月后,我终于把它放在一起了。它确实工作得很好。再次感谢。
      【解决方案3】:

      @Matthew - 完美的解决方案 - 作为倾向于破坏 MVC 概念的代码的替代方案,您可以使用:

      <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>  
      <%@ Import Namespace="System.ServiceModel.Syndication" %>
      

      【讨论】:

        【解决方案4】:

        使用 MVC,您甚至不需要创建视图,您可以使用 SyndicationFeed 类直接将 XML 返回到提要阅读器。

        (编辑).NET ServiceModel.Syndication - Changing Encoding on RSS Feed 这是一个更好的方法。 (而是从这个链接中截取。)

        http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx

        public ActionResult RSS(string id)
        {            
             return return File(MyModel.CreateFeed(id), "application/rss+xml; charset=utf-8");
        }
        

        在我的模型中

        CreateFeed(string id)
        { 
                SyndicationFeed feed = new SyndicationFeed( ... as in the MS link above)
        
                .... (as in the MS link)
        
                //(from the SO Link)
                var settings = new XmlWriterSettings 
                { 
                   Encoding = Encoding.UTF8, 
                   NewLineHandling = NewLineHandling.Entitize, 
                   NewLineOnAttributes = true, 
                   Indent = true 
                };
                using (var stream = new MemoryStream())
                using (var writer = XmlWriter.Create(stream, settings))
                {
                    feed.SaveAsRss20(writer);
                    writer.Flush();
                    return stream.ToArray();
                 }
        
        
        }
        

        【讨论】:

          猜你喜欢
          • 2011-04-08
          • 2011-07-23
          • 1970-01-01
          • 2010-09-24
          • 2015-06-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多