0

I'm following a tutorial where the author inherits from ActionResult to create RssViewResult class that will return RSS. When I run the code and go to the action method that returns RssViewResult as a result the plain XML file appeares. I'm new to RSS and seems like in this tutorial there is no special view that has been created.
Do I need to create a view or I made a mistake in my code?
Here is the code:

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);
        }
    }
}

And here is the action method that returns 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);
    }
4
  • Where are u viewing the XML ? Commented Jan 4, 2013 at 12:20
  • I think nowhere) Can You explain how to create a view that will render rss? Commented Jan 4, 2013 at 12:32
  • 1
    Firefox detects RSS feeds and does render them accordingly. I know for sure that chrome needed a plugin till few months back. Commented Jan 4, 2013 at 12:35
  • thank You! everything works. Commented Jan 4, 2013 at 12:38

1 Answer 1

2

The XML is the view itself!

Firefox detects the XML and renders it as a RSS reader would. I know for sure that Chrome needed a plugin (but this was 6 months back when I last checked).

You can style RSS with CSS for the RSS readers by adding

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

Update

This question and its related answer How to Add CSS Reference to .NET SyndicationFeed? gives a good reference about how to add css using SyndicationFeed

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.