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) %> <%= 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> { } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net-mvc – 在MVC5中呈现Razor的部分视图
- asp.net-mvc – ModelState.IsValid是假的 – 但是哪一个 –
- asp.net-mvc – ASP.NET MVC;使用EditorTemplates编辑集合,
- Asp.net会员密码
- ASP.NET Core根据环境切换NLog配置
- ASP.NET页面事件 – 在GridView绑定之后发生按钮单击事件
- asp.net-mvc-4 – 用于空响应的ASP.NET Web API和状态代码
- asp.net中ScriptManager和ScriptManagerProxy之间的区别
- 是否可以在IIS 7中将虚拟目录设置为根目录?
- asp.net-mvc – MVC 3 Url Helper提供不正确的URL