Skip to content

Instantly share code, notes, and snippets.

@codereflection
Created September 9, 2010 16:40
Show Gist options
  • Save codereflection/572134 to your computer and use it in GitHub Desktop.
Save codereflection/572134 to your computer and use it in GitHub Desktop.
using System;
using Castle.MonoRail.Framework;
using System.Xml.Serialization;
namespace Ris.Operations.Oats.Presentation.Attributes
{
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)]
public class XlsReturnBinderAttribute : Attribute, IReturnBinder
{
private IEngineContext engineContext;
private IControllerContext controllerContext;
private Type returnType;
private object returnValue;
public void Bind(IEngineContext engineContext, IController controller,
IControllerContext controllerContext, Type returnType, object returnValue)
{
this.returnValue = returnValue;
this.returnType = returnType;
this.controllerContext = controllerContext;
this.engineContext = engineContext;
ProcessResponse();
}
private void ProcessResponse()
{
ModifyControllerContext();
RenderXls();
CancelRenderView();
SetContentType();
}
private void RenderXls()
{
var serializer = new XmlSerializer(returnType);
serializer.Serialize(engineContext.Response.OutputStream, returnValue);
}
private void ModifyControllerContext()
{
controllerContext.LayoutNames = null;
}
private void CancelRenderView()
{
controllerContext.SelectedViewName = null;
}
private void SetContentType()
{
engineContext.Response.ContentType = "application/xls";
engineContext.Response.AppendHeader("Content-Disposition", "inline; filename=\"download.xls\"");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment