加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在MVC ASP.NET中使用/显示RSS源的简单方法

发布时间:2020-12-15 19:27:24 所属栏目:asp.Net 来源:网络整理
导读:我是MVC框架的新手,并且想知道如何将RSS数据从控制器传递给视图.我知道需要转换成一个IEnumerable列表.我看到一些创建一个匿名类型的例子,但是无法弄清楚如何将RSS源转换为通用列表并将其传递给视图. 我不希望它是强类型的,因为将有多个调用各种RSS源. 有什
我是MVC框架的新手,并且想知道如何将RSS数据从控制器传递给视图.我知道需要转换成一个IEnumerable列表.我看到一些创建一个匿名类型的例子,但是无法弄清楚如何将RSS源转换为通用列表并将其传递给视图.

我不希望它是强类型的,因为将有多个调用各种RSS源.

有什么建议么.

解决方法

我一直在玩一个在MVC中做WebParts的方式,基本上是UserControls包装在一个webPart容器中.我的一个测试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;

        }
    }
}

UserControl

<%@ 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>
    {
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读