asp.net-mvc – 在MVC ASP.NET中使用/显示RSS源的简单方法
发布时间:2020-05-23 04:48:06 所属栏目: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>
{
}
} (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- dependency-injection – Ninject:构造函数参数
- asp.net-mvc – Firefox在Ajax请求重定向期间不保留自定义标
- NHibernate中对同一个对象的Lazyload要设置一致
- asp.net – 如果找不到图像src,则隐藏input =“image”
- asp.net – WebMethod未被Visual Studio 2013中的PageMetho
- 如何在asp.net网站上使用图片资源?
- asp.net-mvc – HttpContext和HttpContextWrapper之间的区别
- asp.net-mvc – 如何在asp.net中使用Captcha mvc
- asp.net – 如何在EF DbContext和AspNet Membership之间共享
- asp.net后台cs中的JSON格式变量在前台Js中调用方法(前后台示
