我正在學習一個教程,其中作者從ActionResult繼承來創建將返回RSS的RssViewResult類。當我運行代碼並轉到返回RssViewResult的action方法時,出現純XML文件。我是RSS新手,看起來在本教程中沒有創建特殊視圖。
我是否需要創建視圖或者在代碼中犯了錯誤?
下面是代碼:在Asp.Net中生成RSS MVC 3返回純文本XML
public class RssViewResult : ActionResult
{
public RssViewResult(SyndicationFeed feed)
{
RssFeed = feed;
}
public SyndicationFeed RssFeed { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var resp = context.HttpContext.Response;
resp.ContentType = "application/rss+xml";
var rss = new Rss20FeedFormatter(RssFeed);
using (var xml = XmlWriter.Create(resp.Output))
{
rss.WriteTo(xml);
}
}
}
這裏是返回RSS操作方法:
public RssViewResult RssFeed()
{
var feed = new SyndicationFeed("News", "Store news",
new Uri(Request.Url.AbsoluteUri))
{
Items = _itemsRepository
.GetItems()
.Select(i =>
new SyndicationItem(
i.Product.Name,
i.Product.Description,
new Uri(Request.Url.AbsoluteUri)))
};
return new RssViewResult(feed);
}
你在哪裏查看XML? – frictionlesspulley
我覺得無處)你能解釋一下如何創建一個可以渲染RSS的視圖嗎? –
Firefox檢測RSS提要並相應地呈現它們。直到幾個月前,我肯定知道chrome需要一個插件。 – frictionlesspulley