Timmy's Blog

ASP.NET MVC - Creating a custom ActionResult

 

5/10/2008 19:17:37


Controller actions in an ASP.NET MVC application return an instance of a class
which derives from the ActionResult class, usually an instance of the ViewResult class.
Other classes which derive from the same base class are EmptyResult, RedirectResult,
RedirectToRouteResult, JsonResult and ContentResult.

We needed the ability to send a file to the browser and none of the existing classes
is well suited for that, so I created a custom ActionResult and called it FileActionResult.

public class FileActionResult : ActionResult { public FileActionResult() { } public byte[] Data { get; set; } public string Mimetype { get; set; } public string Filename { get; set; } public override void ExecuteResult(ControllerContext context) { if (Data == null) throw new ArgumentNullException("Data"); if (Mimetype == String.Empty) throw new ArgumentException("Mimetype"); if (Filename == String.Empty) throw new ArgumentException("Filename"); context.HttpContext.Response.Clear(); context.HttpContext.Response.ClearContent(); context.HttpContext.Response.ClearHeaders(); context.HttpContext.Response.ContentType = Mimetype; context.HttpContext.Response.AppendHeader("Content-Disposition", String.Format("attachment, filename={0}", Filename)); context.HttpContext.Response.BinaryWrite(Data); } }

As you can see, it's a very trivial task to create a custom ActionResult,
using it is just as easy. Consider the following method in a Controller:

public FileActionResult Download(string Id, string Format) { GoogleDocumentManager manager = new GoogleDocumentManager(); byte[] data = null; string mimetype = string.Empty; string filename = string.Empty; switch (Format) { case "odf": data = manager.GetDocumentAs(eGoogleExportType.OpenOffice, Id, out mimetype, out filename); break; case "txt": data = manager.GetDocumentAs(eGoogleExportType.PlainText, Id, out mimetype, out filename); break; case "doc": data = manager.GetDocumentAs(eGoogleExportType.MicrosoftWord, Id, out mimetype, out filename); break; case "rtf": data = manager.GetDocumentAs(eGoogleExportType.RichText, Id, out mimetype, out filename); break; default: data = manager.GetDocumentAs(eGoogleExportType.PDF, Id, out mimetype, out filename); break; } return new FileActionResult() { Mimetype = mimetype, Filename = filename, Data = data }; }

The code looks at the format specified in the url, for example:
http://www.somedomain.com/document/download?format=pdf,
downloads the file from the Google servers and creates a new FileActionResult.

I realize this isn't much of a guide, but I'm very busy at the moment...
still I hope this will useful for some of you :-)

If you want to learn more about the MVC framework, check out
the blog of the MVC / ASP.NET Guru: Scott Guthrie

PS: Quite a few people have been asking me about the Google code,
and some have even submitted patches for it, so I'll probably publish
it some time soon... even thought it's a quick hack.
(Google will add official support to their .NET API client anyway)


Comments:

 

On 5/10/2008 19:20:15, Lien wrote:

Chinees dus! :-D
 

On 28/12/2008 19:30:26, salah wrote:


 
Your comments:
Your details:
 
   
 

Since you have not authenticated,
we require you to submit some
additional information and fill out the captcha.

Your e-mail address will not be disclosed to anyone and will not be visible on the site. If you specify your blog, we will display a link to it.

If you sign in with your OpenID, you can store your profile and you will never have to enter your details or fill in the captcha again.