I've create a SitemapResult
class that derives from ActionResult
. It allows the caller to add any number of URL resources, and it then outputs sitemap data in XML format.
public class SitemapResult : ActionResult
{
private List<SitemapUrl> SitemapItems;
public SitemapResult()
{
SitemapItems = new List<SitemapUrl>();
}
public void AddUrl(string url, DateTime? lastModified = null, SitemapFrequency? frequency = null, double? priority = null)
{
AddUrl(new SitemapUrl(url, lastModified, frequency, priority));
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "text/xml; charset=utf-8";
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
// TODO: Write sitemap data to output
}
}
}
The problem is that the class stores all the URLs until ExecuteResult()
is called. It seems like it would be more efficient if I could write each URL to the response as they are added rather than hold them all in memory and then write every thing at once.
Does anyone know of any good examples of overriding ActionResult
to write data to the response as it becomes available? In this case, I would think ExecuteResult()
won't need to write anything at all.