MVC contains a nice class you can inherit inorder to return http responses other then a regular Render View:
bellow are three implementations I use the most:
ImageResult, TextResult and RssResult:
public class ImageResult : ActionResult
{
public Stream ImageStream { get; private set; }
public string ContentType { get; private set; }
public ImageResult(Stream imageStream, string contentType)
{
if (imageStream == null)
throw new ArgumentNullException("imageStream");
if (contentType == null)
throw new ArgumentNullException("contentType");
this.ImageStream = imageStream;this.ContentType = contentType;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");
HttpResponseBase response = context.HttpContext.Response;response.ContentType =
this.ContentType;
byte[] buffer = new byte[4096];
while (true)
{
int read = this.ImageStream.Read(buffer, 0, buffer.Length);
if (read == 0)
break;
response.OutputStream.Write(buffer, 0, read);
}
response.End();
}
}
public class TextResult : ActionResult
{
public string Content { get; private set; }public TextResult(string content)
{
this.Content = content;
}
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
throw new ArgumentNullException("context");HttpResponseBase response = context.HttpContext.Response;
response.Write(Content);
response.End();
}
public class RssResult : ActionResult
{
public SyndicationFeed Feed { get; set; }
public RssResult() { }
public RssResult(SyndicationFeed feed)
{
this.Feed = feed;
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ContentType = "application/rss+xml";
Rss20FeedFormatter formatter = new Rss20FeedFormatter(this.Feed);
using (XmlWriter writer = XmlWriter.Create(context.HttpContext.Response.Output))
{
formatter.WriteTo(writer);
}
}
}