2013-01-04 39 views
0

我正在學習一個教程,其中作者從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); 
    } 
+0

你在哪裏查看XML? – frictionlesspulley

+0

我覺得無處)你能解釋一下如何創建一個可以渲染RSS的視圖嗎? –

+1

Firefox檢測RSS提要並相應地呈現它們。直到幾個月前,我肯定知道chrome需要一個插件。 – frictionlesspulley

回答

2

的XML是視圖本身!

Firefox檢測到XML並將其呈現爲RSS閱讀器。我確信Chrome需要一個插件(但是這是在我上次檢查時的6個月之前)。

您可以通過添加

<?xml-stylesheet type= "text/css" href="rss.css"?> 

更新

這個問題及其相關答案How to Add CSS Reference to .NET SyndicationFeed?提供了有關如何使用添加CSS一個很好的參考樣式與CSS的RSS閱讀器RSS SyndicationFeed

+0

在我的代碼中,我可以放這個嗎?我的意思是css –

+0

我更新了我的答案和你想要的CSS部分 – frictionlesspulley