Aleksei Chepovoi
Aleksei Chepovoi

Reputation: 3955

Generating RSS in Asp.Net MVC 3 returns plain XML

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

Upvotes: 0

Views: 1018

Answers (1)

frictionlesspulley
frictionlesspulley

Reputation: 12368

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

Upvotes: 2

Related Questions