简体   繁体   中英

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?

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

If you have your data serialized, just return the content-type (eg 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM