10

私は MVC フレームワークが初めてで、RSS データをコントローラーからビューに渡す方法を知りたいと思っています。ある種の IEnumerable リストに変換する必要があることはわかっています。匿名型を作成する例をいくつか見てきましたが、RSS フィードを汎用リストに変換してビューに渡す方法がわかりません。

さまざまな RSS フィードへの複数の呼び出しがあるため、強く型付けしたくありません。

助言がありますか。

4

4 に答える 4

10

私は、基本的に webPart コンテナーにラップされた UserControls である MVC で WebParts を実行する方法をいじっています。私のテスト UserControls の 1 つは Rss フィード コントロールです。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>
    {
    }
}
于 2008-11-07T16:11:47.237 に答える
6

@Matthew - 完璧なソリューション - MVC の概念を破る傾向があるコード ビハインドの代替として、次を使用できます。

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<SyndicationFeed>" %>  
<%@ Import Namespace="System.ServiceModel.Syndication" %>
于 2010-06-30T04:42:12.397 に答える
1

MVC を使用すると、ビューを作成する必要さえありません。SyndicationFeed クラスを使用して、XML をフィード リーダーに直接返すことができます。

(編集) .NET ServiceModel.Syndication - RSS フィードのエンコーディングを変更するこれはより良い方法です。(代わりにこのリンクから切り取ってください。)

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");
}

MyModel 内

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();
         }


}
于 2012-01-12T16:32:29.443 に答える
0

RSS は、特別な形式の xml ファイルです。その一般的な形式でデータセットを設計し、ReadXml メソッドで rss(xml) を読み取り、ファイルへのパスとして uri を読み取ることができます。次に、別のクラスから使用できるデータセットを取得します。

于 2008-11-07T08:03:26.937 に答える