I attempted to create an RSS in an MVC 2.0 application using my typical C# Web forms approach and received the following error:  OutputStream is not available when a custom TextWriter is used.

The main reason for this is that MVC Views assume the request will be in standard HTML and so it switches the TextWriter with its own writer.

The easiest way to get this done, then, is to handle the logic at the Controller level, rather than the View level.

1) First you will need to include a couple of libraries:

using System.Xml;
using System.ServiceModel.Syndication;

2) Then you want to create a custom RssActionResult, which the Controller will return:

public class RssActionResult : ActionResult
{
public SyndicationFeed Feed { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = “application/rss+xml”;

        Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
rssFormatter.WriteTo(writer);
}
}
}

3) Then you want to set the Controller response to your custom result:

SyndicationFeed feed = new SyndicationFeed(
“IDEAInbox”,
“IDEAInbox Description”,
new Uri(“
http://IDEAInbox”),
“IDEAInboxSyndicationV1.0”,
DateTime.Now);
List<Idea> _Ideas = _myIdeaInboxservice.GetIdeas(-1, -1, 1);
List<SyndicationItem> items = new List<SyndicationItem>();
foreach (Idea _Idea in _Ideas)
{
SyndicationItem item = new SyndicationItem(_Idea.IdeaSubject,
_Idea.IdeaDescription,
new Uri(_Idea.Link),
“IDEAInboxIdeaID” + _Idea.IdeaID.ToString(),
_Idea.StatusInformation.CreateDateTime);
items.Add(item);
}
feed.Items = items;
return new RssActionResult() { Feed = feed };

Advertisements