MVC 使用 FileResult 创建 RSS/Atom Feed
MVC Create RSS/Atom Feed with FileResult
我尝试为我的 ASP.NET MVC 5 网站创建 RSS 提要。我创建了一些 classes 来创建带有 XmlSerializer
的 XML。我在派生自 FileResult
:
的特殊 Result
class 中使用此序列化程序
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; }
}
然后我写了一个Controller
的扩展方法:
public static RssResult RssFeed(this Controller controller, RssFeed feed, string FileDownloadName = "feed.rss")
{
return new RssResult()
{
Feed = feed,
FileDownloadName = FileDownloadName
};
}
如果我调用 returns a RssResult
Firefox 和 Internet Explorer 要求我下载文件的操作。但我想看看浏览器的典型 reader 界面。
我在这里做错了什么我必须改变什么?
FileResult 添加一个 content-disposition
header 以在浏览器中提示 "download"。
如果您的数据已序列化,只需 return content-type
(例如 application/xml
或您已有的数据 (application/rss+xml
)...所以 return Content
或派生自 ActionResult
看到这个 XMLResult example from MvcContrib - 注意它派生自 ActionResult
(不是 FileResult
)
第...
我尝试为我的 ASP.NET MVC 5 网站创建 RSS 提要。我创建了一些 classes 来创建带有 XmlSerializer
的 XML。我在派生自 FileResult
:
Result
class 中使用此序列化程序
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; }
}
然后我写了一个Controller
的扩展方法:
public static RssResult RssFeed(this Controller controller, RssFeed feed, string FileDownloadName = "feed.rss")
{
return new RssResult()
{
Feed = feed,
FileDownloadName = FileDownloadName
};
}
如果我调用 returns a RssResult
Firefox 和 Internet Explorer 要求我下载文件的操作。但我想看看浏览器的典型 reader 界面。
我在这里做错了什么我必须改变什么?
FileResult 添加一个 content-disposition
header 以在浏览器中提示 "download"。
如果您的数据已序列化,只需 return content-type
(例如 application/xml
或您已有的数据 (application/rss+xml
)...所以 return Content
或派生自 ActionResult
看到这个 XMLResult example from MvcContrib - 注意它派生自 ActionResult
(不是 FileResult
)
第...