Skip to content

Instantly share code, notes, and snippets.

@cmatskas
Created September 18, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save cmatskas/bd87a36da0a5d4993a2b to your computer and use it in GitHub Desktop.
Save cmatskas/bd87a36da0a5d4993a2b to your computer and use it in GitHub Desktop.
XmlHttpHandler.cs
public class MySuperDuperHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
var configService = new ConfigService();
var page = new OutputCachedPage(new OutputCacheParameters
{
Duration = 300, // in seconds
Location = OutputCacheLocation.Server,
VaryByParam = "None"
});
page.ProcessRequest(HttpContext.Current);
XDocument doc = GetXmlData();
context.Response.ContentType = "text/xml";
context.Response.ContentEncoding = System.Text.Encoding.UTF8;
context.Response.Expires = -1;
context.Response.Cache.SetAllowResponseInBrowserHistory(true);
doc.Save(context.Response.Output);
}
public bool IsReusable
{
get { return false; }
}
/// <summary>
/// Code required for server-side caching
/// </summary>
private sealed class OutputCachedPage : Page
{
private readonly OutputCacheParameters cacheSettings;
public OutputCachedPage(OutputCacheParameters cacheSettings)
{
// Tracing requires Page IDs to be unique.
ID = Guid.NewGuid().ToString();
this.cacheSettings = cacheSettings;
}
protected override void FrameworkInitialize()
{
base.FrameworkInitialize();
InitOutputCache(cacheSettings);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment