Search code examples
c#asp.net-mvcbrowserrssatom-feed

MVC Create RSS/Atom Feed with FileResult


I try to create a RSS feed for my ASP.NET MVC 5 web site. I created some classes to create the XML with a XmlSerializer. I use this serializer inside a special Result class which is derivated from FileResult:

public class RssResult : FileResult
{
    public RssResult() : base( "application/rss+xml") { }

    protected override void WriteFile(HttpResponseBase response)
    {
        var seri = new XmlSerializer(typeof(RssFeed));
        seri.Serialize(response.OutputStream, this.Feed);
    }

    public RssFeed Feed { get; set; }
}

Then I wrote an extension method for Controller:

public static RssResult RssFeed(this Controller controller, RssFeed feed, string FileDownloadName = "feed.rss")
{
    return new RssResult()
    {
        Feed = feed,
        FileDownloadName = FileDownloadName
    };
}

If I call an action that returns a RssResult Firefox and Internet Explorer asks my to download the file. But I want to see the typical reader interfaces of the browsers.

What I am doing wrong here respectively what I must change?


Solution

  • FileResult adds a content-disposition header that prompts the "download" in a browser.

    If you have your data serialized, just return the content-type (e.g. application/xml or what you already have (application/rss+xml)...so either return Content or derive from ActionResult

    See this XMLResult example from MvcContrib - note that it derives from ActionResult (not FileResult)

    Hth...